Android 具有最大化宽度和复选框的布局
Android layout with maximized width and checkbox
如何制作如下图所示的布局 - 最大化到父元素,但仅限于宽度,包裹的元素将适合右侧。我最终得到了如下 xml 中的布局,但是当 TextView 较长时,它会与复选框重叠。
<?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">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
在您的 TextView 中,添加以下规则:
android:toLeftOf="@+id/checkbox"
最后我用 LinearLayout
和 android:layout_weight="1"
做了这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
如何制作如下图所示的布局 - 最大化到父元素,但仅限于宽度,包裹的元素将适合右侧。我最终得到了如下 xml 中的布局,但是当 TextView 较长时,它会与复选框重叠。
<?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">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
在您的 TextView 中,添加以下规则:
android:toLeftOf="@+id/checkbox"
最后我用 LinearLayout
和 android:layout_weight="1"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>