2013-08-05 24 views
89

Tôi đang cố gắng đặt giữa một số ImageView bên trong một số LinearLayout theo chiều ngang và chiều dọc, nhưng tôi không thể thực hiện được. Lý do chính khiến tôi không sử dụng RelativeLayout vì lý do tôi cần layout_weight (Activity của tôi bao gồm bốn cột phải được chia đều và cũng đáp ứng với chiều rộng màn hình khác nhau, mỗi cột có ImageView được căn giữa và không được mở rộng) .Làm cách nào để căn giữa nội dung bên trong bố cục tuyến tính?

Đây là xml của tôi cho đến nay:

<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

bài ảnh chụp màn hình như bạn muốn và ảnh chụp màn hình hiện tại –

Trả lời

228

android:gravity xử lý các liên kết của nó trẻ em,

android:layout_gravity xử lý căn chỉnh của chính nó.

Vì vậy, hãy sử dụng một trong các tùy chọn này.

<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> 

hoặc

<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

Cảm ơn, người đàn ông! Nó hoạt động hoàn hảo! – horta

+0

"android: layout_gravity tự lo liệu." sẽ sửa đổi để nói trọng lực bố trí được sử dụng bởi phụ huynh. – ataulm

25
android:layout_gravity is used for itSelf of layout 

sử dụng android:gravity="center" cho Childs của bạn LinearLayout

do đó, mã của bạn nên được

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

Tôi đã thử các giải pháp ở đây nhưng tôi có vấn đề và thử một số thay đổi mà tôi nhớ những sai lầm trong layout_width cần thiết wrap_content như thế này

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