2015-02-23 80 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> 
相關問題