我需要把CardView作为我动态创建的安卓系统中的单选按钮的背景。我已经为单选组编写了xml,我正在创建单选按钮并动态添加它们。现在我需要把单选按钮的背景,这将是卡片视图。那么我该怎么做呢?我可以使用appCompatRadioButton1.setBackgroundResource(R.drawable.csr_icon);在后台添加一个图标
我知道我们可以创建自定义模板并添加到此单选按钮的背景中,但背景文件必须在可绘制文件夹中,而它是具有卡片视图的布局文件。
发布于 2016-10-25 03:52:55
您可以拥有一个带有FrameLayout的CardView,然后将该RadioGroup作为子级添加到CardView中的FrameLayout中。
为了避免麻烦,我只需要将RadioGroup的xml作为一个CardView,其中包含带有id的RadioGroup。那么访问无线电群组就是在CardView上使用findViewById
假设您使用的是SDK 20及更高版本,请使用以下代码。通过findViewById加载单选按钮。这不是专门的cardView,而是模拟CardView背景
单个单选按钮文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:elevation="2dp"
android:background="@drawable/temp">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a temp string for radio"/>
</FrameLayout可绘制的临时表格如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="8dp" />
</shape>https://stackoverflow.com/questions/40226017
复制相似问题