我有一些按钮而不是使用ListView,所以我想为每个按钮添加一个CheckBox,但我不知道如何。
如您所知,CheckBox本身就是一个Button。
此时我很困惑:我不知道如何制作它
以下是我希望它的例子:
如果你能帮助我,我真的很感激。
答案 0 :(得分:1)
我会在这里给你一些一般性指导,因为问题很广泛。
如您所知,CheckBox
本身继承自Button
。这意味着你只需要勾选一个复选框"看起来"像一个满足要求的按钮。您需要做的就是扩展CheckBox
类
public class CheckBoxButton extends CheckBox {
}
当然你需要实现构造函数和东西。您需要做的最重要的事情是覆盖onDraw
。在那里,你可以在它周围画一个边框。并且可能画一个阴影来使它成为3D。你可以做任何事情让它看起来像一个按钮。
有关自定义绘图的更多信息,请访问:http://developer.android.com/training/custom-views/custom-drawing.html
或者,您可以找到一个库来执行此操作。 Android Arsenal是找个人的好地方。
答案 1 :(得分:0)
我会使用带有TextView和空CheckBox的LinearLayout。由于您使用的是ListView,因此您应该在适配器中添加OnItemClickListener以处理每个视图中的点击,因此您不需要使用按钮。
如果您确实需要/想要使用Button,请使用FrameLayout代替列表中的每个项目,因此复选框可以位于按钮上方。像(未经测试)的东西:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:layout_marginRight="10dp"/>