我有一个项目列表,每个项目基本上是两个用视图分隔符分隔的复选框。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- VIEW 1 -->
<CheckBox android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="VIEW 1"
android:background="@drawable/checkbox_background"/>
<!-- SEPARATOR -->
<View android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"/>
<!-- VIEW 2 -->
<CheckBox android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="VIEW 2"
android:background="@drawable/checkbox_background"/>
</LinearLayout>
drawable/checkbox_background.xml
是一个有两个实心形状的选择器 - 白色表示未选中状态,红色表示已选中状态。
这就是列表视图的样子:
我想要实现的是在检查其相邻复选框时删除分隔符(事实上,我有两个以上):
根据我的理解,我的选择是:
当复选框设置为选中状态时,将分隔符的可见性设置为View.GONE
。但是它迫使view1和view2重新绘制并将它们的文本移近中心,这看起来并不好看。
使用负边距按分隔符的宽度在边缘重叠view1和view2,然后将分隔符放在它们上面。然后我可以成功View.INVISIBLE
。但是这种技术需要RelativeLayout,我不能使用RelativeLayout,因为我需要在屏幕上均匀地分隔我的复选框。
添加另一个(红色)分隔符并一次显示一个。但这只是丑陋。
更改分隔符LayoutParams
(高度)和背景。到目前为止,这可能是我最好的选择。
有没有更好的解决方案?也许有些人不需要以编程方式做事?
感谢。
答案 0 :(得分:1)
您可以使用LinearLayout
中的复选按钮嵌套RelativeLayout
并将分隔线放在它们上面:
<?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="wrap_content">
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- VIEW 1 -->
<CheckBox
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/checkbox_background"
android:text="VIEW 1"/>
<!-- VIEW 2 -->
<CheckBox
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/checkbox_background"
android:text="VIEW 2"/>
<!-- SEPARATOR -->
<View
android:layout_width="2dp"
android:layout_height="0dp"
android:layout_alignBottom="@id/parent"
android:layout_alignTop="@id/parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@android:color/holo_green_light"/>
</RelativeLayout>
如果您不需要精确的8dp边距并且可以为分隔线设置固定高度,则可以使用FrameLayout
。它具有更好的性能,因为它的视图不那么复杂:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- checkboxes -->
</LinearLayout>
<!-- SEPARATOR -->
<View
android:layout_width="2dp"
android:layout_height="16dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@android:color/holo_green_light"/>
</FrameLayout>