android - 带按钮的自定义 View

标签 android android-custom-view layer-list

我需要构建以下 View :

enter image description here

按钮可以是使用 findViewById() 方法从 java 代码访问的常规按钮。

我不确定我是否可以使用 LayerList 来做到这一点或者我是否需要从头开始实现自定义 View ?任何人都可以建议一些我可以遵循的途径来实现这一目标吗?

更新

现在我有这个:

enter image description here

我现在需要做的是: - 隐藏圈外的东西; - 只有 cicle 内的按钮部分应该触发 onClick 事件。

我应该使用自定义 View 组来做到这一点吗?我试过了,但我无法在按钮顶部绘制圆圈(应该可以在 onDraw() 方法中完成吗?)...

这是我的代码:

ma​​in_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>

最佳答案

您必须结合两件事:

  1. 通过用 ImageView(上层)覆盖您的按钮(下层)来构建布局,其中有一个洞。 See here, how to do that .这两层可以用FrameLayout

  2. 定义
  3. 现在您必须将圆圈上的所有触摸委托(delegate)给按钮。这应该可以通过在 ImageView 上设置一个 TouchListener 来实现。如果触摸在圆圈外,则此 Touchlistener 返回 true,否则返回 false。返回 false 时,Android 应该将触摸向下传递到下一层,即带有按钮的层。

总而言之,我希望结果是值得的,因为这似乎需要大量的工作和测试工作。

关于android - 带按钮的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777887/

相关文章:

android - 对于低于 23 的 API,图层列表中的可绘制形状呈现不正确

android - 具有顶部/底部属性的项目的层列表

android - 将滚动事件传递给另一个 View

android - 共享首选项 xml 文件

android - 使用可跨越字符串更改阿拉伯语变音符号颜色在某些手机上不起作用

android - 减小 AlertDialog.Builder 组件的字体大小

android - 安卓游戏中的通知

android - 找出哪个 View 消耗了触摸事件,如果有的话

java - 避免 Android 中绘制/布局操作期间出现对象分配错误

android - 如何在图层列表中制作阴影?