【问题标题】:Give all child elements fixed width in Android在Android中给所有子元素固定宽度
【发布时间】:2014-08-13 20:14:59
【问题描述】:

如何在LinearLayout 内为所有元素提供固定宽度?想象一下,我有一个按钮列表,我会将它们全部居中,并根据具有最大宽度的元素给它们相同的宽度,有什么办法吗?让布局管理器动态地执行此操作(我的意思是不要为每个按钮重复代码)?我的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center">
    <Button android:id="@+id/mainStageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/mainStage"/>          
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/amphiteatre"/>    
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/aula" />
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/mensa"/>  
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:onClick="menuClickListener"
        android:text="@string/sportsSchedule"/> 

【问题讨论】:

  • 听起来不错,但我想问是否有办法在 LayoutManager 标记中设置它(你知道,DRY 原则 :))
  • 我将移除我的 cmets,因为它们无济于事。我很快就会添加另一个。
  • 谢谢,我也编辑了我的问题,也许我没有说清楚:)
  • 我相信您当前的设置无法修改以产生所需的结果。这是我的建议:您的父容器将是 FrameLayout - {width:match, height:match}。在FrameLayout 内,放置一个LinearLayout - {width:wrap, height:**,orientation:vertical, layout_gravity:center_horizo​​ntal}。将您的Buttons 放在此LinearLayout 中,但将其width 属性更改为match_parent 并删除layout_gravity 属性。
  • 很好,真的很管用!将您的评论放入答案中,以便我可以正确地为您投票:)

标签: java android android-layout


【解决方案1】:

您考虑过使用 listView 吗?您可以使用具有更简洁代码库的 listView 来实现您正在寻找的结果:

List View

【讨论】:

    【解决方案2】:

    您可以使用 for 循环并通过 view. getChildAt(index) 遍历所有 Layout 的子级:)

    【讨论】:

    • 这可行,但我想在我的 XML 布局中设置它。
    【解决方案3】:

    最靠谱的方法:

    1. 编写自定义布局管理器

    2. 或者更简单,选择一个现有的布局管理器来满足您的需求

    在这种情况下,TableLayoutLinearLayout 的子类,它已经具有正确的属性。

    来自链接:

    一列的宽度由该列中单元格最宽的行定义

    只需将线性布局替换为:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="This is rather long" >
            </Button>
        </TableRow>
    
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medium" >
            </Button>
        </TableRow>
    
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Short" >
            </Button>
        </TableRow>
    </TableLayout>
    

    结果如下图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2012-03-30
      • 2022-08-19
      • 1970-01-01
      相关资源
      最近更新 更多