Android: Как сделать все элементы внутри LinearLayout одинакового размера? - PullRequest
77 голосов
/ 24 июля 2009

Я хотел бы создать диалоговое окно для отображения заголовка видео и тегов. Ниже текста я хотел бы добавить кнопки View, Edit и Delete и сделать эти элементы одинакового размера. Кто-нибудь знает, как изменить XML-файл макета, чтобы сделать элементы внутри LinearView одинакового размера?

Текущий файл макета выглядит следующим образом:

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

    <LinearLayout 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:orientation="vertical">

          <TextView 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:id="@+id/txtTitle" android:text="[Title]" >
          </TextView>

          <TextView 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:id="@+id/txtTags"            
              android:text="[Tags]" >
          </TextView>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <Button 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:id="@+id/btnPlay" 
           android:text="View">
        </Button>

        <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/btnEdit" 
            android:text="Edit">
        </Button>

        <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/btnDelete" 
            android:text="Delete">
        </Button>

    </LinearLayout>

</LinearLayout>

Буду признателен, если кто-нибудь сможет показатьрешение путем изменения содержимого вставленного файла.

Спасибо!

Ответы [ 3 ]

170 голосов
/ 24 июля 2009

Используйте android:layout_width="0px" и android:layout_weight="1" на трех Button с. Это говорит о том, что кнопки должны занимать не более 0 пикселей, но три из них должны разделить любое дополнительное пространство между ними. Это должно дать вам визуальный эффект, который вы хотите.

32 голосов
/ 20 августа 2010

Другой способ - сделать android:layout_width="fill_parent" и android:layout_weight="1", это также будет хорошо работать !!!

18 голосов
/ 05 июня 2015

Используйте LinearLayout с желаемым weightSum и создайте элементы с равным layout_weight . Вот пример ...

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_share_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_search_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_event_note_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_brush_white_36dp"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_menu_white_36dp"/>
</LinearLayout>

Итак, весовая сумма всех элементов равна 5. Вот скриншот ...

enter image description here

Примечаниечто, используются Google Material Design значки. Надеюсь, что это полезно.

...