代码之家  ›  专栏  ›  技术社区  ›  Pochmurnik SK Developers

单选按钮不显示

  •  -1
  • Pochmurnik SK Developers  · 技术社区  · 7 年前

    我在做“高级安卓开发课程”的练习。此代码不显示 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 . 怎么 文本框 把那些纽扣藏起来了?我也试过用 marginEnd

    5 回复  |  直到 7 年前
        1
  •  2
  •   Sniffer    7 年前

    对TextView的高度和宽度使用wrap content,如下所示。

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

    你可以使用 权重属性 要根据视图的权重为其分配一部分布局空间,并且为了获得更好的响应,请尝试使用 约束布局 . 要了解有关ConstraintLayout的更多信息,请单击 Here

        2
  •  4
  •   AskNilesh    7 年前

    你的 TextView match_parent 这就是为什么你 RadioButton 不可见

    试着设置 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>
    
        3
  •  2
  •   shkschneider blazzerbg    7 年前

    LinearLayout orientation="horizontal" 你的第一次 TextView 带全宽 layout_width="match_parent" 没有其他视野。

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

        4
  •  2
  •   Ratilal Chopda    7 年前

    试试这个

    <?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>
    
        5
  •  1
  •   Cindy Meister    7 年前

    本物业转让 textView

    android:layout_width="match_parent"
    

    分配 wrap_content