将CheckBox可绘制图像填充到ListView适配器中的CheckBox宽度

时间:2016-06-15 04:57:05

标签: android

我有一个listView适配器,我在checkbox中设置了buttonDrawable图像。我已将checkbox的宽度设置为listView的1/3。但我无法缩放适合复选框宽度的复选框可绘制图像。我没找到任何东西

customAdapter

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    int width = viewGroup.getWidth();
    View rowView;
    rowView = inflater.inflate(R.layout.singlerow_compare, null);
    holder.checkBox = (CheckBox) rowView.findViewById(R.id.imageViewCompare);
    holder.checkBox.setButtonDrawable(imageId[i]);
    holder.checkBox.setWidth(width/3);
}

    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        //I am using this
        }
    });

   //This is not working
   rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("error", "notworking");
            //this is not working so I am using checkbox onclick listener
        }

的ListView

        ListView listView = (ListView)findViewById(R.id.listView);
        customAdapter = new CustomAdapter(this, prgmNameList, prgmImages);
        listView.setAdapter(customAdapter);

singlerow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/singleRow" android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageViewCompare"
        android:gravity="center"
        android:layout_alignLeft="@+id/imageViewCompare"
        android:layout_alignRight="@+id/imageViewCompare"
        android:id="@+id/textViewCompare" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewCompare"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:button="@drawable/ic_launcher"
        android:gravity="center"
        android:focusableInTouchMode="false"/>

</RelativeLayout>

enter image description here

2 个答案:

答案 0 :(得分:0)

这样做:

 <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewCompare"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@drawable/ic_launcher"
    android:gravity="center"
    android:button="@null"
    android:focusableInTouchMode="false"/>

而不是设置android:button将其设置为android:background

编辑1:设置

android:button="@null"

答案 1 :(得分:0)

使用Imageview下方的checkboxscaleType centercrop,以使其符合复选框          

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageViewCompare"
        android:gravity="center"
        android:layout_alignLeft="@+id/imageViewCompare"
        android:layout_alignRight="@+id/imageViewCompare"
        android:id="@+id/textViewCompare" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@+id/ic_launcher"
        android:layout_alignLeft="@+id/imageViewCompare"
        android:layout_alignRight="@+id/imageViewCompare"
        android:layout_alignTop="@+id/imageViewCompare"
        android:layout_alignBottom="@+id/imageViewCompare"
        android:gravity="center"
        android:focusableInTouchMode="false"/> 

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewCompare"
        android:gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:focusableInTouchMode="false"/>

</RelativeLayout>