android - 合并 View 的 layout_weight 和 maxWidth

标签 android layout css landscape-portrait

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

这是一个示例布局:

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

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

最佳答案

在对相对布局进行了一番折腾之后,我偶然发现了 this answer ,这是我最终使用的。

关于android - 合并 View 的 layout_weight 和 maxWidth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6284911/

相关文章:

android - 我无法将 Android 项目从 Delphi 10.4 迁移到 Delphi 11.1

java - 将日期和时间字符串转换为特定格式的日期

android - 在BottomSheetDialogFragment内安排布局,以使所有 View 均符合预期

java - 在 StringTemplate 电子邮件中使用 css

css - 使用封面和附加大量图像时背景图像失真

android - volley 库中的 JsonObjectRequest 出错

ios - Collection View 布局问题

css - webkit 空 td 为 1px

javascript - 循环播放的 HTML5 视频在循环播放时出现黑屏

android - getExternalCacheDir() 如何在 Android 上运行?