Android 单选按钮不显示
我正在做“高级Android开发课程”的练习。此代码不显示单选按钮:Android 单选按钮不显示,android,android-radiobutton,Android,Android Radiobutton,我正在做“高级Android开发课程”的练习。此代码不显示单选按钮: <?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" an
<?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
时出现。TextView
如何隐藏这些按钮?我也尝试过使用marginEnd
或weight atributes,但这不是一个解决方案。您的文本视图
高度是匹配您的父母
,这就是为什么您的RadioButton
不可见的原因
尝试在文本视图中设置android:layout\u 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>
试试这个
<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>
对文本视图的高度和宽度使用换行内容,如下所示
您的线性布局是方向=“水平”
但是您的第一个文本视图使用layout\u width=“match\u parent”
全幅显示,没有为任何其他视图留出空间
检查您的布局。我不确定您是否希望TextView
垂直位于单选按钮上方
此TextView
的属性分配不正确
android:layout_width="match_parent"
改为分配wrap\u内容
,它将起作用。maketextview
widthwrap\u内容
。在示例中,按钮位于文本旁边,我怎样才能把它们移到左边?@Pochmunik没有得到你能解释更多吗?我想我应该改变TextView的宽度和高度来包装内容,而不是添加另一个属性。TextView占据了整个片段,我只是把match_parent改为包装内容。@Pochmunik如果你想在版面中添加更多视图,你需要根据你的要求设置权重