如何在android中创建六边形视图菜单

时间:2015-02-06 05:47:57

标签: android android-layout android-imageview android-custom-view android-custom-attributes

我打算创建一个带有图标的六边形视图菜单,如下图所示。我曾尝试使用github中的library进行imageview,但根据我的要求,它并不适合我,我需要的是六边形视图以及特定六边形区域中的点击事件。该库为单个图像目标提供了六边形视图,但考虑到六边形菜单,它无法给出六边形输出(即,我无法在我的布局中正确对齐)。

请帮助我提出改进布局或代码的建议。提前致谢。

enter image description here

2 个答案:

答案 0 :(得分:2)

我尝试使用ImageViews,LinearLayout和应用负边距来创建您需要的布局。

我不确定它是否是理想的使用方式,但我想你可以尝试一下,如果不想去画布。

请仔细阅读以下示例......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical">

  <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"/>

        </LinearLayout>



    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="-25dp">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:layout_marginLeft="12.5dp"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"
/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:layout_marginLeft="-25dp"/>

    </LinearLayout>




    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="-25dp">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="-25dp">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:layout_marginLeft="12.5dp"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"
            />
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:layout_marginLeft="-25dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="-25dp">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/hexa"
            android:visibility="invisible"/>

    </LinearLayout>
</LinearLayout>

此处drawable/hexa是单个白色六边形png图像。

希望它有所帮助...

答案 1 :(得分:1)

下面附有六边形视图的代码。为方便起见,更改按钮图像

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.example.me.testapplication.Buttons">


<LinearLayout
    android:id="@+id/ll1"
    android:layout_width="211dp"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"

    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@drawable/cell"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/cell" />

</LinearLayout>

<LinearLayout
    android:id="@+id/ll3"
    android:layout_width="216dp"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_gravity="right|top"

    android:layout_toEndOf="@+id/ll2"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@drawable/cell"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/cell" />
</LinearLayout>

<LinearLayout
    android:id="@+id/ll2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal|top"

    android:layout_toEndOf="@+id/ll1"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cell"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/cell" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cell" />
</LinearLayout>


</FrameLayout>