【问题标题】:Android LayoutWeight = 1 of RadioButton in RBGroupAndroid LayoutWeight = RBGroup 中 RadioButton 的 1
【发布时间】:2017-09-03 02:48:36
【问题描述】:

这是布局

<FrameLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tab"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
           <LinearLayout
                android:id="@+id/panel1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textMultiLine"
                    android:text="How much is 5 by 5?"
                    android:visibility="visible" />
                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <RadioButton
                        android:id="@+id/radio_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="10" />
                    <RadioButton
                        android:id="@+id/radio_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="25" />
                    <RadioButton
                        android:id="@+id/radio_3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="50" />
            </RadioGroup>
        </LinearLayout>
    </FrameLayout>

我需要的是:EditBox 位于顶部,所有单选按钮将共享 EditText 下方的其余空间。为此我使用android:layout_weight="1"

当 Android 应用布局管理器从 XML 加载此布局时,无线电会按预期使用空间。

当我使用以下代码构建它时:

rb.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

它没有.. 无线电都向上嵌套,而不是散布在空间的其余部分中。 请问,知道为什么吗?我做错了什么?

谢谢。

【问题讨论】:

  • what am I doing wrong? 1 - 摆脱无用的 FrameLayout。嵌套布局不利于性能。保持您的设计尽可能平坦2 - 权重大小必须恰好为 0 dp,才能使权重起作用。
  • 这不是没用的。这段代码将被放置在另一个容器 - FrameLayout 中。也许这很糟糕,但对我来说是必要的部分之一。问题是 - 看我的回答。
  • FrameLayout + LinearLayout... 您可以简单地使用 PercentRelativeLayout 并拥有一个 single 容器。
  • 在这个问题中,我简化了结构。在 FrameLayout 和 Linear 之间有 ViewAnimator )。我动态创建了一些线性,在 Animator 中加载它们,所有的花束都留在 Frame 中

标签: android radio-button radio-group android-layout-weight


【解决方案1】:
  1. 删除所有radiobittons的android:layout_weight;
  2. 将 android:layout_weight=1 设置为 edittext 和 radiogroup

【讨论】:

  • 它们是在运行时设置的。
【解决方案2】:

答案是下一个:

如果使用这些参数的正确结构,RadioGroup 中 RadioButton 的 LayoutWeight 将正确工作。 在我的情况下是:

rb.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
                                        ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多