2013-08-05 88 views
89

我試圖在LinearLayout的水平和垂直方向居中放置一個ImageView,但我無法做到。 我之所以不使用RelativeLayout的主要原因是因爲我需要layout_weight(我的Activity由四列組成,這四列應該平分,並且對不同的屏幕寬度作出響應,每列都有一個集中和未拉伸的ImageView) 。如何將內容置於線性佈局中?

這是到目前爲止我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000" 
    android:baselineAligned="false" 
    android:gravity="center" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Main" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageButton_speak" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_speak" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageButton_readtext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_readtext" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageButton_edit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_edit" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageButton_config" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_config" /> 
    </LinearLayout> 
</LinearLayout> 
+0

後截屏爲你想要和當前屏幕截圖 –

回答

228

android:gravity處理的對準其兒童,

android:layout_gravity處理其自身的對齊。

所以使用其中之一。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000" 
    android:baselineAligned="false" 
    android:gravity="center" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Main" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/imageButton_speak" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_speak" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/imageButton_readtext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_readtext" /> 
    </LinearLayout> 

    ... 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000" 
    android:baselineAligned="false" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Main" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageButton_speak" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_speak" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageButton_readtext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@drawable/image_bg" 
      android:src="@drawable/ic_readtext" /> 
    </LinearLayout> 

    ... 
</LinearLayout> 
+3

謝謝,男人!它非常完美! – horta

+0

「android:layout_gravity照顧好自己。」將修改爲說母版使用版面重力。 – ataulm

25
android:layout_gravity is used for itSelf of layout 

爲孩子的使用android:gravity="center"LinearLayout

所以你的代碼應該是

<LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1" > 
4

我試過的解決方案在這裏,但我有問題,並嘗試一些改變我介意錯layout_width需要是wrap_content這樣

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_weight="1" > 
相關問題