单选按钮不显示

RadioButtons not displaying

我正在做 "Advanced Android Development Course" 的练习。此代码不显示 RadioButtons:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/my_fragment_color"
   android:orientation="horizontal"
   tools:context=".SimpleFragment">

<TextView
    android:id="@+id/fragment_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
    android:padding="10dp"
    android:text="@string/question_article"/>

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio_button_yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:text="@string/yes"/>

    <RadioButton
        android:id="@+id/radio_button_no"
        android:layout_marginRight="8dp"
        android:text="@string/no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RadioGroup>

当我删除 TextView 时出现

RadioButtonsTextView 如何隐藏这些按钮?我也尝试使用 marginEnd 或权重属性,但这不是解决方案。

你的 TextView 身高是 match_parent 这就是你的 RadioButton 不可见的原因

尝试在您的 TextView 中设置 android:layout_weight="1" 它会起作用

试试这个

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/my_fragment_color"
        android:orientation="horizontal"
        tools:context=".SimpleFragment">

        <TextView
            android:id="@+id/fragment_header"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:padding="10dp"
            android:text="@string/question_article"/>

        <RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radio_button_yes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:text="@string/yes"/>

            <RadioButton
                android:id="@+id/radio_button_no"
                android:layout_marginRight="8dp"
                android:text="@string/no"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RadioGroup>
</LinearLayout>

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/fragment_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="10dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:text="@string/question_article"/>

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio_button_yes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:text="@string/yes"/>

        <RadioButton
            android:id="@+id/radio_button_no"
            android:layout_marginRight="8dp"
            android:text="@string/no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>




</LinearLayout>

对 TextView 的高度和宽度使用环绕内容,如下所示。

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_btn_bg_color"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/fragment_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:padding="10dp"
        android:text="question_article"/>
    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/radio_button_yes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="yes"/>
        <RadioButton
            android:id="@+id/radio_button_no"
            android:text="no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>
</LinearLayout>

您可以使用 weight 属性 将布局的一部分 space 分配给一个视图,这取决于它的权重,并且为了更好地响应 UI 尝试使用 ConstraintLayout。要了解有关 ConstraintLayout 的更多信息,请单击 Here

您的 LinearLayoutorientation="horizontal",但您的第一个 TextView 占满宽度,layout_width="match_parent" 没有 space 用于任何其他视图。

检查您的布局。我不确定你是否想要 TextView 垂直上方 RadioButton.

textView 的 属性 分配不正确

android:layout_width="match_parent"

改为分配 wrap_content,它将起作用。