首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓:如何让LinearLayout中的所有元素都相同大小?

安卓:如何让LinearLayout中的所有元素都相同大小?
EN

Stack Overflow用户
提问于 2009-07-24 11:15:15
回答 3查看 80.3K关注 0票数 81

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

当前布局文件如下所示:

代码语言:javascript
复制
<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>

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

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-07-24 11:30:01

在三个Button上使用android:layout_width="0px"android:layout_weight="1"。这意味着按钮占用的空间不能超过0像素,但这三个按钮之间应该分开任何额外的空间。这应该会给你带来你想要的视觉效果。

票数 178
EN

Stack Overflow用户

发布于 2010-08-20 19:30:27

另一种方法是使android:layout_width="fill_parent"android:layout_weight="1",这也会很好!

票数 33
EN

Stack Overflow用户

发布于 2015-06-05 14:59:54

LinearLayout与所需的weightSum一起使用,并使用相等的layout_weight创建元素。这里有一个例子..。

代码语言:javascript
复制
<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。这是屏幕截图…

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

票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1177020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档