【问题标题】:Combine layout_weight and maxWidth for views为视图组合 layout_weight 和 maxWidth
【发布时间】:2011-09-11 05:04:14
【问题描述】:

我试图让我的布局更适合横向,我的一个常见模式是添加一个带有一些按钮的LinearLayout(例如 OK | Cancel),然后为 layout_weight 设置一个值,以便空间是均匀分布的。问题是,当您在横向模式下使用手机或(尤其是)平板电脑时,您最终会得到看起来很可笑的巨大按钮。我尝试在按钮和线性布局上设置maxWidth,但无济于事。实现这一目标的最简单方法是什么?即,设置视图的最大水平尺寸,使其不会增长到整个宽度。我尝试了很多不同的方法,但都没有奏效。

这是一个示例布局:

    <LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:paddingLeft="10dp"
      android:paddingRight="10dp"
      android:paddingTop="6dp"
      android:paddingBottom="6dp">
      <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:enabled="false"
        android:textStyle="bold"
        android:text="@string/button1" />
      <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button2" />
    </LinearLayout>

注意:我知道我可以为横向创建一个新的布局文件并在那里做魔术。我希望将专门的布局文件保持在最低限度,我希望这不需要它们。

【问题讨论】:

    标签: android layout css landscape-portrait


    【解决方案1】:
    <RelativeLayout
       android:id="@+id/rlMain"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_marginLeft="12dp"
       android:layout_marginRight="12dp">
    
        <View
          android:id="@+id/vCenter"
          android:layout_width="0dp"
          android:layout_height="0dp"
          android:layout_centerHorizontal="true"
          android:layout_marginRight="5dp"
          android:layout_marginLeft="5dp"/>
    
        <Button
         android:id="@+id/btnOne"
         android:layout_width="wrap_content"
         android:layout_height="40dp"
         android:layout_toLeftOf="@id/vCenter"
         <!--Change this for different widths-->
         android:minWidth="200dp"/>
    
        <Button
         android:id="@+id/btnTwo"
         android:layout_width="wrap_content"
         android:layout_height="40dp"
         android:layout_toRightOf="@id/vCenter"
         <!--Change this for different widths-->
         android:minWidth="200dp"/>
    </RelativeLayout>
    

    【讨论】:

    • 为您的答案添加一点解释对未来的读者更好?
    【解决方案2】:

    在对相关布局进行了相当多的处理之后,我偶然发现了this answer,这就是我最终使用的。

    【讨论】:

    • 我假设您的意思是该页面上的“扩展 LinearLayout 并覆盖 onMeasure 函数”答案。该页面有一个更新的答案,“添加外部布局”stackoverflow.com/questions/5875877/…,它更简单(但我没有测试它)。
    【解决方案3】:

    我这里没有 Eclipse 来测试它,但我会使用 RelativeLayout 来代替,例如:

    <RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      >
      <View
        android:id="@+id/centerView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        />
      <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/centerView"
        android:enabled="false"
        android:textStyle="bold"
        android:text="@string/button1" 
        />
      <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/centerView"
        android:text="@string/button2" 
        />
    </RelativeLayout>
    

    正如我所说,我现在无法对其进行测试,但您可以解决这个想法。

    附带说明,除非您的布局非常简单,否则我通常会为横向创建单独的布局。这需要更多的工作,但您可以通过这种方式更好地优化视图。特别是,如果您计划支持更大的屏幕。

    【讨论】:

    • 顺便说一句,这是否符合cheating 的条件? ;)
    • 嘿,不,这很好。我正在尝试它并且工作得很好,除了RelativeLayout 吃了我上面的EditText。但是,它看起来很有希望!
    • @dmon 您必须将android:layout_below="@id/textView1"(或任何ID)添加到每个视图。 RelativeLayout 是一个非常强大的布局。查看所有可能的 attrs here
    • Oy,我终于让它工作了,但是这些 RelativeLayouts 很痛苦。我还是更喜欢LinearLayouts 可以如此轻松地分配空白空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多