为视图组合layout_weight和maxWidth

dmo*_*mon 11 css layout android landscape-portrait

我正在尝试使我的布局更加横向友好,我有一个常见的模式是添加LinearLayout一些按钮(例如OK |取消),只需设置一个值,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>
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*dam 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>
Run Code Online (Sandbox Code Playgroud)

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

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