【问题标题】:Android: How to make all elements inside LinearLayout same size?Android:如何使 LinearLayout 中的所有元素大小相同?
【发布时间】:2010-11-13 16:53:30
【问题描述】:

我想创建一个对话框来显示视频标题和标签。在文本下方,我想添加按钮查看、编辑和删除,并使这些元素大小相同。有谁知道如何修改 .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>

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

谢谢!

【问题讨论】:

    标签: android layout


    【解决方案1】:

    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。这是截图...

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您对这个答案感到非常兴奋。 :-) 热爱热情!
      • CommonsWare 的解决方案对我不起作用,但它做到了! :) 我使用了“match_parent”,因为我听说你应该更喜欢它而不是“fill_parent”,但两者都有效。
      • 这些元素的 LinearLayout 父级还必须具有 layout_width="match_parent"
      【解决方案3】:

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

      【讨论】:

      • 如果你真的想要你也可以使用带有 android:stretchColumns="0,1,2" 的 TableLayout。
      • 太棒了...所以这意味着,如果我们将宽度设置为 0px,重量会覆盖设置吗?为什么会这样?
      • 如果您的按钮位于GridLayout 中,这将不起作用。在这种情况下,它们真的变成了0px 宽。也许GridLayout 不接受layout_weight
      • @Zelphir: GridLayout 绝对不使用layout_weight。这仅由LinearLayout 使用,可能还有LinearLayout 的一些子类。
      猜你喜欢
      • 1970-01-01
      • 2014-10-20
      • 2013-04-29
      • 2010-11-16
      • 1970-01-01
      • 2012-02-20
      • 2020-02-14
      • 1970-01-01
      • 2019-09-29
      相关资源
      最近更新 更多