Android ListView中的自定义复选框

Android ListView中的自定义复选框,android,android-layout,listview,checkbox,android-listview,Android,Android Layout,Listview,Checkbox,Android Listview,在我的应用程序中,我有一个带有自定义行的列表视图,我想为每个项目添加一个自定义复选框,如下所示: 当我点击“编辑”时,我想将列表视图转换为右侧,并在每个列表视图行的左侧显示一个自定义复选框 即使看不到某些行图标(如星形或箭头),是否可以保留右侧的列表视图?或者如果我翻译右侧的列表视图,行被压缩,所有图标都可见 我已将一个翻译图像: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas

在我的应用程序中,我有一个带有自定义行的
列表视图
,我想为每个项目添加一个自定义
复选框
,如下所示:

当我点击“编辑”时,我想将
列表视图
转换为右侧,并在每个
列表视图
行的左侧显示一个自定义
复选框

即使看不到某些行图标(如星形或箭头),是否可以保留右侧的
列表视图
?或者如果我翻译右侧的
列表视图
,行被压缩,所有图标都可见

我已将一个
翻译图像

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:fromXDelta="0%"
    android:toXDelta="17%"
    android:duration="750" />

</set>

是的,请选中一个复选框,如
gone
。并设置为您的信息视图,如下所示:

android:layout_width="0dp"
android:layout_weight="1"
此视图的父视图应该是带有
android:orientation=“horizontal”的
LinearLayout

custom\u checkbox.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" 
          android:drawable="@xml/checked" /> 
    <item android:state_pressed="true"
          android:drawable="@xml/checked" /> 
    <item android:drawable="@xml/unchecked" /> 
</selector>


checked.xml

    <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#ff2665B4" android:endColor="#ff2665B4" android:angle="270"/>
    <stroke android:width="4px" android:color="#ffc0c0c0" />
    <size android:height="20dp" android:width="20dp"/>
</shape>
    <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#ffffff" android:endColor="#ffffff" android:angle="270"/>
    <stroke android:width="4px" android:color="#ffc0c0c0" />
    <size android:height="20dp" android:width="20dp"/>
</shape>


unchecked.xml

    <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#ff2665B4" android:endColor="#ff2665B4" android:angle="270"/>
    <stroke android:width="4px" android:color="#ffc0c0c0" />
    <size android:height="20dp" android:width="20dp"/>
</shape>
    <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#ffffff" android:endColor="#ffffff" android:angle="270"/>
    <stroke android:width="4px" android:color="#ffc0c0c0" />
    <size android:height="20dp" android:width="20dp"/>
</shape>

设置为复选框

facilityCheck.setButtonDrawable(R.xml.custom_checkbox);


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@color/backgr_greysoft" >

<CheckBox 
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/chk_facility"
           android:layout_alignParentLeft="true"
           />

      <ImageView
         android:id="@+id/icon"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_gravity="center"
         android:layout_margin="@dimen/rowDescargas_marginIcono"
         android:layout_toRightOf="@+id/chk_facility" />
 </RelativeLayout>
facilityCheck.setButtonDrawable(R.xml.custom_复选框);

不完全是您想要的,但应该给您一个公平的想法,我应该为复选框设置什么宽度和重量?因为如果我为另一个视图设置了weight=1…对于
复选框
set
android:layout\u width=“wrap\u content”
。我会在最后一行获得一个“NullPointerException”。。。(3个文件放在'res/xml'文件夹中)facilityCheck是您在listview行xml文件中的复选框。我已经知道了,我的适配器类有问题。你知道我应该放多少重量吗?还是不称重?我认为没有必要称重。请给我您的行xml文件。