我需要构建以下 View :
按钮可以是使用 findViewById()
方法从 java 代码访问的常规按钮。
我不确定我是否可以使用 LayerList 来做到这一点或者我是否需要从头开始实现自定义 View ?任何人都可以建议一些我可以遵循的途径来实现这一目标吗?
更新
现在我有这个:
我现在需要做的是:
- 隐藏圈外的东西;
- 只有 cicle 内的按钮部分应该触发 onClick
事件。
我应该使用自定义 View 组来做到这一点吗?我试过了,但我无法在按钮顶部绘制圆圈(应该可以在 onDraw()
方法中完成吗?)...
这是我的代码:
main_layout.xml
<?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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="@layout/custom_layout" />
</LinearLayout>
</RelativeLayout>
custom_layou.xml
<?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/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="14dp"
android:paddingBottom="10dp"
android:background="#CC0000FF"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button4" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/buttons_layout"
android:layout_alignLeft="@+id/buttons_layout"
android:layout_alignBottom="@+id/buttons_layout"
android:src="@drawable/circle_foreground" />
</RelativeLayout>
circle_foreground.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="100dp"
android:thickness="0dp"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke android:width="4dp"
android:color="#000000"/>
</shape>
最佳答案
您必须结合两件事:
通过用
ImageView
(上层)覆盖您的按钮(下层)来构建布局,其中有一个洞。 See here, how to do that .这两层可以用FrameLayout
定义
现在您必须将圆圈上的所有触摸委托(delegate)给按钮。这应该可以通过在
ImageView
上设置一个TouchListener
来实现。如果触摸在圆圈外,则此Touchlistener
返回 true,否则返回 false。返回 false 时,Android 应该将触摸向下传递到下一层,即带有按钮的层。
总而言之,我希望结果是值得的,因为这似乎需要大量的工作和测试工作。
关于android - 带按钮的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777887/