RadioButtons не отображается

Я делаю упражнение из «Продвинутого курса разработки под Android». Этот код не отображает 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 или атрибуты веса, но это не было решением.

сделайте textview шириной wrap_content.

Hemant Parmar 20.08.2018 11:16
1
1
168
5
Перейти к ответу Данный вопрос помечен как решенный

Ответы 5

Ваша высота 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>

В примере кнопки расположены рядом с текстом, как их переместить влево?

Pochmurnik 20.08.2018 11:16

@pochmurnik не понял тебя, можешь объяснить больше?

AskNilesh 20.08.2018 11:17

Я думаю, мне следует изменить ширину и высоту TextView на wrap_content, чем добавить еще один атрибут.

Pochmurnik 20.08.2018 11:41

TextView взял весь фрагмент, я просто изменил match_parent на wrap_content.

Pochmurnik 20.08.2018 11:45

@pochmurnik вам нужно установить вес в соответствии с вашими требованиями, если вы хотите добавить больше просмотров в макет

AskNilesh 20.08.2018 11:49

Попробуй это

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

Вы можете использовать атрибут веса, чтобы выделить часть пространства макета для представления, в зависимости от его веса, а для лучшего отзывчивого пользовательского интерфейса попробуйте использовать ConstraintLayout. Чтобы узнать больше о ConstraintLayout, нажмите Здесь

Ваш LinearLayout - это orientation = "horizontal", но ваш первый TextView занимает всю ширину, а layout_width = "match_parent" не оставляет места для любого другого просмотра.

Проверьте свой макет. Я не уверен, хотите ли вы, чтобы TextView располагался вертикально над RadioButton.

Это свойство для textView присвоено неверно

android:layout_width = "match_parent"

Назначьте вместо него wrap_content, и он будет работать.

Другие вопросы по теме