Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有按钮的自定义视图_Android_Android Custom View_Layer List - Fatal编程技术网

Android 带有按钮的自定义视图

Android 带有按钮的自定义视图,android,android-custom-view,layer-list,Android,Android Custom View,Layer List,我需要构建以下视图: 其中按钮可以是使用findViewById()方法从java代码访问的常规按钮 我不确定我是否可以使用或是否需要从头实现自定义视图?有谁能提出一些我可以实现这一目标的途径吗 更新 现在我有这个: 我现在需要做的是: -隐藏圈外的东西; -只有cicle内的部分按钮才能触发onClick事件 我应该使用自定义视图组来执行此操作吗?我试过了,但我无法在按钮顶部绘制圆圈(应该可以在onDraw()方法中这样做吗?) 这是我的密码: main_layout.xml <?x

我需要构建以下视图:

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

我不确定我是否可以使用或是否需要从头实现自定义视图?有谁能提出一些我可以实现这一目标的途径吗

更新

现在我有这个:

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

我应该使用自定义视图组来执行此操作吗?我试过了,但我无法在按钮顶部绘制圆圈(应该可以在
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>
<?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>
<?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>

自定义布局.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>
<?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>
<?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>

circle_front.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>
<?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>
<?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>

您可以使用6个不同的imageview,并在relativelayout中组合成一个圆形imageview,
在每个imageview中,设置其onclicklistener,以便您可以执行所需的操作

必须结合两件事:

  • 通过将按钮(下层)与带有孔的
    图像视图(上层)重叠来构建布局。这两个层可以用
    FrameLayout

  • 现在,您必须将圆上的所有接触委派给按钮。这应该可以通过在
    ImageView
    上设置
    TouchListener
    来实现。如果触摸在圆圈外,则此
    Touchlistener
    返回true,否则返回false。当返回false时,Android应该将触摸传递到下一层,即带有按钮的层


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

    我不确定你所说的“…在relativelayout中合并到一个圆形图像视图”是什么意思。你能解释一下我怎么做吗?我可能错过了一些简单的东西:s