Android:如果背景存在,则复选框不存在

时间:2014-06-24 20:09:31

标签: android api checkbox background visible

我正在使用ListView创建一个Activity。每行由两个TextView和一个CheckBox组成。问题是,如果我将背景属性添加到复选框,它不会显示在行中(不是因为它是透明的)。 有人有解释吗?

实际上它在API 19上都可以正常工作,但测试是在带有API 11的模拟器上进行的,我想要以API 8 +为目标。

提前谢谢。

没有背景属性的图片。

具有背景属性的图像(您可以看到字符串一直持续到帧的结尾):

[由于我的声誉而无法加载图片...]

代码如下。

提前谢谢你。

MainActivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView) findViewById(R.id.lvListaMyLocationSalvate);

    ArrayList<String[]> array = new ArrayList<String[]>();
    array.add(new String[]{"asdasdasdasdasdasdasdasdasdasdasdasdasd","asdasdasdasdasdasdasdasdasdasdasdasdasd"});
    array.add(new String[]{"asdasdasdasdasdasdasdasdasdasdasdasdasd","asdasdasdasdasdasdasdasdasdasdasdasdasd"});

    Adapter arAdapter = new Adapter(this, array);
    listView.setAdapter(arAdapter);
}
}

适配器:

public class Adapter extends ArrayAdapter<String[]>{

Activity context;
ArrayList<String[]> array;

public Adapter(Activity activity, ArrayList<String[]> array) {
    super(activity, R.layout.row, array);
    this.context = activity;
    this.array = array;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if(view == null) {
        view = context.getLayoutInflater().inflate(R.layout.row, null);
    }

    TextView tv1 = (TextView) view.findViewById(R.id.tvRow1);
    TextView tv2 = (TextView) view.findViewById(R.id.tvRow2);
    CheckBox cb = (CheckBox) view.findViewById(R.id.cb);
    tv1.setText(((String[])(array.get(position)))[0]);
    tv2.setText(((String[])(array.get(position)))[1]);
    cb.setChecked(false);

    return view;
}
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
    android:id="@+id/lvListaMyLocationSalvate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</RelativeLayout>

row.xml(CheckBox背景属性有所不同)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<CheckBox
    android:id="@+id/cb"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_centerInParent="true"
    android:background="#000000"        
    android:focusable="false"
    android:focusableInTouchMode="false" />

<TextView
    android:id="@+id/tvRow1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_toLeftOf="@id/cb"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tvRow2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_below="@id/tvRow1"
    android:layout_toLeftOf="@id/cb" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

CheckBox的默认背景是@android:drawable / btn_check_label_background。如果您查看\ platforms \ android-11 \ data \ res \ drawable-hdpi文件夹中的drawable,您将找到一个九个补丁图像,它有效地定义了CheckBox的最小高度和宽度,并添加了一些填充文本,使CheckBox的复选框部分不会遇到文本。通过将背景更改为颜色(而不是定义任何文本),不再有最小高度或宽度,从而有效地隐藏了CheckBox。

如果您想要黑色背景,则需要将btn_check_label_background图像复制到res \ drawable-hdpi文件夹,然后对其进行修改,使您想要黑色的位为黑色。有关9-patch drawable如何工作的更多详细信息,请参阅http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch