2015-02-23 82 views
0

我需要将两个图像放在屏幕中央的下方。他们需要在中心。图像会根据代码而不断变化,所以如何让图像在所有屏幕上都能正常工作而无需移动或尺寸增加或减少?如何在屏幕上的布局内设置图像?

我尝试使用网格视图,但它似乎太多代码的两个图像。它的工作原理,但有什么更高效?

回答

2

对于这种类型的设计,我总是创建屏幕中心点,请检查下面的代码 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ImageView 
    android:id="@+id/image1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" 
    android:layout_centerHorizontal="true" 
    android:layout_above="@+id/center_point" /> 

<View 
    android:id="@+id/center_point" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_centerInParent="true"/> 

<ImageView 
    android:id="@+id/image2" 
    android:src="@drawable/ic_launcher" 
    android:layout_below="@+id/center_point" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" /> 

它肯定会工作。

所有最优秀的

+0

你能说你为什么加了标签。如果没有它,它会好吗? – Apurva 2015-02-23 17:06:09

+0

@apurva你会怎么做?给出你的答案。 – Ravi 2015-02-24 04:13:15

+0

图片后图像和设置中心水平都为 – Apurva 2015-02-24 04:17:35

0

如果妳有永远只有2图像,可以在这样的xml文件设置:在父

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/image1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <ImageView 
     android:id="@+id/image2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

第一张图片中心,第二个中心水平,并对齐屏幕的底部。

祝你好运。

0

做一个垂直Linear-layout同时包含您ImageView S的,然后为他们两个平等layout_weight。设置layout_centerInParent真正为您Linear_layout

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/lock_layout_keypad" 
    android:orientation="vertical" 
    android:layout_centerInParent="true" 
    android:layout_marginBottom="4dp"> 


    <ImageView 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:src="@drawable/ic_launcher" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <ImageView 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView2" 
     android:src="@drawable/ic_launcher" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

</LinearLayout> 

您可以删除android:layout_weight

0

这里这段代码可能会帮助你。你必须创建一个LinearLayout并设置他的垂直方向和layout_gravity =居中horizo​​ntal.After创建两个图像视图,这是自动位置在屏幕中心一个在其他下方。我希望这会帮助你。快乐编码:)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_gravity="center_horizontal"> 


    <ImageView 
     android:id="@+id/image_view_two" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     /> 

    <ImageView 
     android:id="@+id/image_view_one" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 
</LinearLayout> 
相关问题