首页 > 解决方案 > 当我使用 customadaptor 将单选按钮添加到列表视图时,单选按钮的布局/样式不同

问题描述

我在列表视图中使用单选按钮和复选框。当我使用自定义适配器填充列表视图时,单选按钮样式与复选框样式一起更改。当我将它添加到 XML 中没有自定义适配器的列表视图时,它的样式是默认的。附上列表视图项的 XML 布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- TextView for displaying question-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal"
        android:weightSum="2">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:text="@string/check"
            android:textColor="@android:color/background_dark" />

        <TextView
            android:id="@+id/question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="50dp"
            android:layout_marginLeft="20dp"
            android:padding="@dimen/activity_horizontal_margin"
            android:layout_gravity="center"
            android:textColor="#000"
            android:textSize="18sp" />

    </LinearLayout>

    <!-- RadioGroup for grouping of RadioButton-->

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal"
        android:weightSum="3">

        <RadioButton
            android:id="@+id/rbutt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="15"
            android:textColor="#000"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/rbutt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="30"
            android:textColor="#000"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/rbutt3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="45"
            android:textColor="#000"
            android:textSize="12sp" />
    </RadioGroup>
</LinearLayout>

将单选按钮添加到列表视图

    //=========================================================================================================
// get the string array from string.xml file
        questions = getResources().getStringArray(R.array.questions);
// get the reference of ListView and Button
        simpleList = (ListView) view.findViewById(R.id.simpleListView);
        submit = (Button) view.findViewById(R.id.submit);
// set the adapter to fill the data in the ListView
        CustomAdapter customAdapter = new CustomAdapter(getContext().getApplicationContext(), questions);
        simpleList.setAdapter(customAdapter);
        //=========================================================================================================

添加了自定义适配器

在 XML 中添加

标签: androidlayoutandroid-listviewradio-button

解决方案


推荐阅读