如何在RadioButton中设置布局而不是文字?

问题描述 投票:0回答:1

一般的单选按钮,都可以有文字,有没有可能像下图所示,添加一个布局而不是文字?

P.S:

我把所有的单选按钮都放在一个单选组里。只是为了布局的缘故,我不想沿着单选组创建一个单独的线性布局。

enter image description here

android android-layout radio-button android-radiobutton
1个回答
1
投票

这个答案也许是多余的,但你可以创建一个自定义的视图,我把你有的东西放在一起,如下图所示

自定义的_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 和回调,你可以创建一个完全可重用的布局,而不是一个radio-group。

© www.soinside.com 2019 - 2024. All rights reserved.