Как установить макеты вместо текста в RadioButton - PullRequest
0 голосов
/ 29 мая 2020

Обычно радиокнопки, вы можете иметь текст вместе с ними, есть ли возможность добавить макет вместо текста, как показано на рисунке ниже?

PS :

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

enter image description here

1 Ответ

1 голос
/ 29 мая 2020

Этот ответ может быть излишним, но вы можете создать собственное представление, я собрал то, что у вас есть, со следующим

custom_radio_view. xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:text="For one hour"
        app:layout_constraintBottom_toBottomOf="@+id/radioButton"
        app:layout_constraintStart_toEndOf="@+id/radioButton"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:text="Until 9:08pm"
        app:layout_constraintBottom_toBottomOf="@+id/radioButton"
        app:layout_constraintStart_toEndOf="@+id/radioButton"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/button"
        style="@style/Widget.AppCompat.Light.ActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        style="@style/Widget.AppCompat.Light.ActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="12dp"
        android:text="-"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

RadioCustomLayout.kt

class RadioCustomLayout: ConstraintLayout {
    constructor(context: Context?): super(context)
    constructor(context: Context?, attrs: AttributeSet?): super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)

    init {
        inflate(context, R.layout.custom_radio_view, this)
    }

}

activity_main. xml

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.test.test.RadioCustomLayout
        android:id="@+id/group1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

С помощью нескольких declare-styleable и обратных вызовов вы можете создать полностью многоразовая раскладка, а не радиогруппа

...