【问题标题】:Custom view with buttons带有按钮的自定义视图
【发布时间】:2013-12-26 00:25:03
【问题描述】:

我需要构建以下视图:

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

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

更新

现在我有这个:

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

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

【问题讨论】:

    标签: android android-custom-view layer-list


    【解决方案1】:

    你必须结合两件事:

    1. 通过将按钮(下层)与ImageView(上层)重叠来构建布局,其中有一个孔。 See here, how to do that。可以使用FrameLayout

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

    总而言之,我希望最终的效果值得付出努力,因为这似乎需要大量的工作和测试工作。

    【讨论】:

      【解决方案2】:

      您可以使用 6 种不同的 imageview 并在 relativelayout 中组合成一个圆形 imageview, 在每个 imageview 中设置他们的 onclicklistener 以便你可以做你想做的事情

      【讨论】:

      • 我不确定“...在 relativelayout 中合并到一个圆形图像视图”是什么意思。你能解释一下我该怎么做吗?我可能错过了一些简单的东西:s
      猜你喜欢
      • 1970-01-01
      • 2013-03-10
      • 2011-12-20
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多