如何将listview中的复选框设置为右侧?

时间:2013-09-21 21:03:24

标签: android android-layout

我在列表视图中使用了以下代码作为视图,我想将复选框设置为右侧,但它不起作用。

<?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">

<TextView 
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"/>
<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    android:gravity="right"
    android:textColor="#000000"/>

5 个答案:

答案 0 :(得分:0)

更改layout_width以换行内容

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:gravity="right"
    android:textColor="#000000"/>

答案 1 :(得分:0)

将其更改为RelativeLayout并将layout_width更改为wrap_content

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

     <TextView 
         android:id="@+id/textView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="#000000"/>
     <CheckBox
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:checked="true"
         android:layout_alignParentRight="true"
         android:textColor="#000000"/>

</RelativeLayout>

答案 2 :(得分:0)

您可以将复选框的宽度更改为wrap_content吗?

如果可以的话,试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">

<TextView 
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:textColor="#000000"/>

甚至这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView 
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:layout_gravity="right"
    android:textColor="#000000"/>

答案 3 :(得分:0)

使用CheckedTextView。右边有复选标记。 Herehere是关于如何使用的2个Google搜索示例。这样,您只需要使用一个视图,而不是当前使用的视图。

答案 4 :(得分:0)

// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="5dp"
    android:gravity="center"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"/>
    </LinearLayout>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textColor="#000000"/>
</LinearLayout>