Android:如何使LinearLayout中的所有元素都相同?

Nik*_*lin 78 layout android

我想创建一个对话框来显示视频标题和标签.在文本下面,我想添加按钮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>
Run Code Online (Sandbox Code Playgroud)

如果有人能通过修改粘贴的文件内容来显示解决方案,我将不胜感激.

谢谢!

Com*_*are 171

使用android:layout_width="0px"android:layout_weight="1"三个Button.这表示按钮不应超过0像素,但其中三个应该分开它们之间的任何额外空间.这应该会给你你想要的视觉效果.

  • 如果你_REALLY_想要你也可以使用带有android:stretchColumns ="0,1,2"的TableLayout. (4认同)

Eby*_*Eby 32

另一种方法是制作android:layout_width="fill_parent",android:layout_weight="1"这也将正常工作!

  • 你对这个答案感到非常兴奋.:-)热爱热情! (17认同)

Mad*_*ota 18

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>
Run Code Online (Sandbox Code Playgroud)

所以,所有元素的权重总和是5.这是截图...

在此输入图像描述

请注意,使用了Google Material Design图标.希望这是有帮助的.