Я делаю упражнение из "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>
RadioButtons
появляется, когда я удаляю TextView
. Как TextView
спрятал эти кнопки? Я пытался использовать также marginEnd
или весовые атрибуты, но это не было решением.
5 ответов
Используйте обтекание содержимого для высоты и ширины 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>
Вы можете использовать весовой атрибут , чтобы выделить часть пространства макета для представления в зависимости от его веса, а для лучшего отзывчивого пользовательского интерфейса попытайтесь использовать ConstraintLayout . Чтобы узнать больше о ConstraintLayout, нажмите здесь
Ваш TextView
высокий match_parent
, поэтому ваш RadioButton
не виден
Попробуйте установить android:layout_weight="1"
в TextView
, это будет работать
Попробуй это
<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>
Ваш LinearLayout
- orientation="horizontal"
, но ваш первый TextView
занимает всю ширину, а layout_width="match_parent"
не оставляет места для любого другого вида.
Просмотрите свой макет. Я не уверен, что вы хотели TextView
вертикально над RadioButton
.
Попробуй это
<?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
является неправильным
android:layout_width="match_parent"
Вместо этого назначьте wrap_content
, и это сработает.
Новые вопросы
android
Android - это мобильная операционная система Google, используемая для программирования или разработки цифровых устройств (смартфоны, планшеты, автомобили, телевизоры, одежда, стекло, IoT). Для тем, связанных с Android, используйте специальные теги Android, такие как android-intent, android-activity, android-адаптер и т. Д. Для вопросов, не связанных с разработкой или программированием, но связанных с платформой Android, используйте эту ссылку: https: // android.stackexchange.com .