结合layout_weight和maxWidth为视图设置宽度

10

我试图使我的布局更适合横向屏幕,常见的方式是添加一个包含一些按钮 (如 确定|取消) 的 LinearLayout,并设置一个 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>

注意:我知道我可以为横屏创建一个新的布局文件并在那里进行更改。我希望尽量减少专门的布局文件数量,并且我希望这不需要它们。

3个回答

1

在尝试了相对布局之后,我偶然发现了this answer,最终我使用了它。


我会假设你指的是那个页面上关于“扩展LinearLayout并重写onMeasure函数”的答案。该页面有一个更新的答案,“添加一个外部布局”https://dev59.com/6G025IYBdhLWcg3wqXxo,这个方法更简单(但我没有测试过)。 - Jerry101

0

我这里没有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>

就像我说的一样,我现在无法测试它,但你可以围绕这个想法进行工作。

顺便说一下,除非你的布局非常简单,否则我通常会为横向创建单独的布局。虽然这需要更多的工作,但你可以更好地优化视图。特别是如果你计划支持更大的屏幕。


顺便问一下,这算作作弊吗?;) - Aleadam
嘿,不错啊。我正在尝试它,效果还不错,除了RelativeLayout吃掉了我上面的EditText之外。但是,看起来很有前途! - dmon
@dmon,你需要在每个视图中添加android:layout_below="@id/textView1"(或者其他ID)来使用RelativeLayout布局。RelativeLayout是一种非常强大的布局方式。你可以在这里查看所有可能的属性链接 - Aleadam
哎呀,我终于让它工作了,但这些RelativeLayout真是令人头疼。我仍然更喜欢LinearLayout可以如此轻松地分配空白空间的方式。 - dmon

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

在回答问题时加上一些解释对于未来的读者会更好吗? - HaveNoDisplayName

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接