【问题标题】:layout_weight in LinearLayout not working - tried everythingLinearLayout 中的 layout_weight 不起作用 - 尝试了一切
【发布时间】:2015-05-16 17:59:03
【问题描述】:

LinearLayout 中的layout_weight 让我抓狂。我在 StackOverflow 和网络上阅读了 reams 的类似问题,但没有一个能解决我的问题。我复制了我遇到的多个样本,但仍然没有乐趣。我的布局(下)很简单

我尝试了多个按钮宽度值(fill_parent、wrap_content、0dp 等)。还尝试在 LinearLayout 中显式设置 weightSum=5.0 -- 所有宽度都相同(坏)的结果。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
> 
<!-- TRIED BUTTON WIDTHS OF fill_parent, wrap_content, 0dp - SAME RESULT! -->
<!-- TRIED weightSum=5.0 FOR LinearLayout - SAME RESUT! -->
<Button 
  android:text="North"
  android:id="@+id/up"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
/>
<Button
  android:text="South"
  android:id="@+id/down"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
/>
  <Button
  android:text="West"
  android:id="@+id/left"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
  />
<Button 
    android:text="East"
    android:id="@+id/right"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
/>
<Button 
  android:text="Exit"
  android:id="@+id/done"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
/>
</LinearLayout>

不知道是否相关,但在我的手机从 KitKat OTA 更新到 Lollipop 后不久就出现了这个问题。另外,我在自定义 Dialog 而非 Activity 中使用此布局,如下所示。

Dlg = new Dialog( Ctx );
Dlg.setContentView(R.layout.nudge );
Dlg.setTitle( "Direction" );
Dlg.setCancelable(false);

下面的第一张图片显示了问题:按钮没有填满布局和文本换行。

奇怪的是,Eclipse 图形布局中的布局看起来很好(第二张图片)。

我在这个问题上花了好几天都没有高兴。 请帮忙!

【问题讨论】:

  • fill_parent弃用。在这种情况下,wrap_content 应该是一个固定值weightSum没用0dp 是正确的(或者权重不起作用)。尝试使用smaller font size
  • 我试过 wrap_content 没有效果。许多人建议 fill_parent 现在称为 match_parent(两者 = -1)。但这也没有用。感谢您的建议。
  • match_parent正确0dp 也是如此。现在,就像我已经建议的那样,只需使用 较小的字体
  • 我遇到了同样的问题。按钮内的文本实际上是这样做的。按照@DerGolem 的建议使用较小的字体。
  • 这对我来说太粗鲁了,无法保持自尊:-)

标签: android android-layout android-linearlayout android-layout-weight


【解决方案1】:

我明白了!问题是(显然)窗口管理器覆盖了根 LinearLayout 中的 fill_parent。为了阻止这种情况,我必须执行以下操作:

            // CREATE DIALOG
            Dlg = new Dialog( Ctx );
            Dlg.setContentView(R.layout.mydialog );
            ...
            // SET DLG TO FULL WIDTH
            Window window = Dlg.getWindow();
            WindowManager.LayoutParams wlp;
            wlp = window.getAttributes();
            wlp.width=WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes( wlp );

这并没有使它成为 100% 全宽,而是接近(我猜是 90%),这对我来说很好。

【讨论】:

    【解决方案2】:

    你几乎成功了。试试看:

    1. 在线性布局中添加 android:weightSum="5"
    2. 在每个按钮中添加 android:testSize="..."。此时,您应该填写这些 testSize 以获得最佳视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 2020-01-30
      • 2012-02-09
      • 1970-01-01
      相关资源
      最近更新 更多