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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1177020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:45:36  来源:igfitidea点击:

Android: How to make all elements inside LinearLayout same size?

androidlayout

提问by Niko Gamulin

I would like to create a dialog to display a video title and tags. Below text I would like to add buttons View, Edit and Delete and make these elements same size. Does anyone know how to modify .xml layout file in order to make elements inside LinearView same size?

我想创建一个对话框来显示视频标题和标签。在文本下方,我想添加按钮查看、编辑和删除,并使这些元素的大小相同。有谁知道如何修改 .xml 布局文件以使 LinearView 中的元素大小相同?

The current layout file looks like this:

当前布局文件如下所示:

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

I would appreciate if anyone could show the solution by modifying the pasted file content.

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

Thanks!

谢谢!

回答by CommonsWare

Use android:layout_width="0px"and android:layout_weight="1"on the three Buttons. That says the buttons should take up no more than 0 pixels, but the three of them should split any extra space between them. That should give you the visual effect you wish.

在三个s上使用android:layout_width="0px"和。也就是说按钮不应占用超过 0 像素,但它们三个应该在它们之间分割任何额外的空间。这应该给你你想要的视觉效果。android:layout_weight="1"Button

回答by Eby

Another way is to make android:layout_width="fill_parent"and android:layout_weight="1"this will also works fine!!!

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

回答by Madan Sapkota

Use LinearLayoutwith your desired weightSumand create elements with equal layout_weight. Here is an example ...

使用带有所需weightSum 的LinearLayout并创建具有相等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>

So, the weight sum of all elements is 5. Here is the screenshot ...

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

enter image description here

在此处输入图片说明

Note that, Google Material Designicons are used. Hope this is helpful.

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