Android 如何在RadioButton中设置布局而不是文本
通常单选按钮可以有文本,是否可以添加布局而不是下图所示的文本 附言: 我把所有单选按钮都保存在一个广播组中。就为了这个布局,我不想沿着无线电组创建单独的线性布局Android 如何在RadioButton中设置布局而不是文本,android,android-layout,radio-button,android-radiobutton,Android,Android Layout,Radio Button,Android Radiobutton,通常单选按钮可以有文本,是否可以添加布局而不是下图所示的文本 附言: 我把所有单选按钮都保存在一个广播组中。就为了这个布局,我不想沿着无线电组创建单独的线性布局 这个答案可能有点过头了,但是你可以创建一个自定义视图,我把你所拥有的与以下内容结合起来 自定义_radio_view.xml activity_main.xml 通过几个declare styleable和回调,您可以创建一个完全可重用的布局,而不是一个无线组 <?xml version="1.0" encoding="utf-8
这个答案可能有点过头了,但是你可以创建一个自定义视图,我把你所拥有的与以下内容结合起来 自定义_radio_view.xml activity_main.xml 通过几个declare styleable和回调,您可以创建一个完全可重用的布局,而不是一个无线组
<?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>
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)
}
}
<?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>