Radiobutton с надписью выше
Я новичок в андроиде, и мне нужно добавить переключатели в свою деятельность, но мне нужно поместить текст на кнопку пули.
Любая помощь, пожалуйста. Я нашел следующее, хотя я не понимаю, что @drawable/main_selector и @style/TabStyle.
Radiobutton с текстом сверху
Может ли кто-нибудь дать мне справочник 101.
UPDATE
Я использовал следующее в соответствии с некоторыми предложениями, но не работал:
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:text="something that is on top"
android:id="@+id/toggle_tab_left"
android:button="?android:attr/listChoiceIndicatorSingle"
style="@null"/>
<RadioButton
android:button="?android:attr/listChoiceIndicatorSingle"
android:text="something that is on top"
android:id="@+id/toggle_tab_right"
style="@null"/>
</RadioGroup>
ОБНОВЛЕНИЕ 2
Я получил свое решение от Warpzit, но до того, как я поставил вопрос на вопрос, может ли кто-нибудь помочь мне в вопросе выравнивания ниже.
У меня будет 5 переключателей в строке, где некоторые из них будут иметь более длинный текст, разделенный на две строки. когда текст поместится на экране, из-за пейзажа или на планшеты, весь текст должен быть в одной строке:
![enter image description here]()
ОБНОВЛЕНИЕ 3
... в зависимости от размера экрана текст может разбиваться на разное количество строк. Это не всегда будет стандартным
![enter image description here]()
Ответы
Ответ 1
@style/TabStyle
- это просто применяемый стиль, вы можете его игнорировать. @drawable/main_selector
- это графическое изображение, которое переключается в зависимости от ситуации. Подробнее о селекторах можно прочитать здесь.
Пример получения текста сверху:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:text="Text on top"
android:button="@null"
android:background="#f00"
android:layout_weight="1"/>
<RadioButton
android:text="Text on top"
android:button="@null"
android:background="#0f0"
android:layout_weight="1"/>
</RadioGroup>
Дает следующий результат:
![Behind, text on top example]()
Если вы хотите, чтобы текст отображался выше, вы можете использовать следующий xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:text="Text on top"
android:button="@null"
android:drawableBottom="@android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1"/>
<RadioButton
android:text="Text on top"
android:button="@null"
android:drawableBottom="@android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1"/>
</RadioGroup>
Это даст следующий результат:
![Above example]()
Ответ 2
Чтобы дополнить отличный ответ Варпзита, вы должны использовать android:gravity="center_horizontal|bottom"
и android:layout_height="match_parent"
на кнопках, чтобы выровнять их.
Также нет необходимости копировать ярлыки переключателей из AOSP, используйте ?android:attr/listChoiceIndicatorSingle
.
![Screenshot]()
XML Layout
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup1"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:text="RadioButton dsfsdfsdfsdfsdf" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:text="RadioButton fdsfsd fdsfsdf fsfsdfs" />
</RadioGroup>
Ответ 3
Хороший способ сделать этот эффект простым в применении - использовать стиль. Для этого поместите этот код в свой styles.xml
под тегом resources
.
<style name="RadioWithTextOnTop">
<item name="android:button">@null</item>
<item name="android:drawableBottom">?android:attr/listChoiceIndicatorSingle</item>
<item name="android:gravity">center_horizontal|bottom</item>
</style>
А затем примените его к своей RadioButton следующим образом:
<RadioButton
style="@style/RadioWithTextOnTop"
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
Ответ 4
Самое простое решение, которое я использовал, это:
<RadioButton
...
android:gravity="center_horizontal|bottom"
android:drawableTop="?android:attr/listChoiceIndicatorSingle"
android:button="@null"
/>
Он также использует более новый стиль анимированной иконки listChoiceIndicatorSingle
, который используется по умолчанию.