为视图组合layout_weight和maxWidth

时间:2011-06-08 20:34:34

标签: android layout css landscape-portrait

我正在尝试使我的布局更加横向友好,我的常见模式是添加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 个答案:

答案 0 :(得分:1)

在对相对布局进行了相当多的讨论之后,我偶然发现了this answer,这是我最终使用的内容。

答案 1 :(得分: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>

正如我所说,我现在无法测试,但你可以解决这个问题。

另外,除非您的布局非常简单,否则我通常会为横向创建单独的布局。这是一个更多的工作,但你可以更好地优化视图。特别是,如果您打算支持更大的屏幕。

答案 2 :(得分:-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>