在安卓系统中是否可以制作一个蜂巢状的XML布局?

7
一个蜂房的布局应该长成这样: 有色的蜂房只是为了让你明白我必须如何摆放元素。
你建议使用哪个Layout小部件?
我尝试使用过GridView,但我无法制作这样的单元格,然后试用了FrameLayout,但不想在设置蜂巢位置时处理像素(或dp)值。
我已经快崩溃了。我接近得出结论,这种东西在Android上无法以高质量的方式完成,而且不能使用类似游戏的库。我希望有人能给我一个好的解决方案。

1
你真的引起了我的好奇心,我进行了一些谷歌搜索,想知道是否可能。一切都表明不行(也就是说,不是通过简单的手段),非矩形布局只能通过“欺骗”来实现,比如https://dev59.com/dWYr5IYBdhLWcg3wNHqh#13879492或https://dev59.com/OnXYa4cB1Zd3GeqP5FZC#19583072 - 换句话说,它们只是看起来是非矩形的,并且在透明区域点击时会被忽略以保持这种印象。然而,在你的情况下,它将不得不委托给相邻的瓷砖,并且处理这个问题将进一步复杂化代码。 - Konrad Morawski
各位,制作蜂巢形状并不是问题所在。那只是比较容易的部分。难点在于如何将元素放置得像蜂巢一样。你们可以看到,每个元素只有一半的高度与另一个元素相接触。因此,主要问题是使用哪种“???Layout”来支持这种元素定位方式。 - sandalone
@MikeM。这正是我想象的,一个解决方案将涉及某种事件总线,通过该总线,组件视图可以订阅事件并间接通知彼此:“这是我的点击吗?如果不是,请委托给这些坐标上找到的任何人”。这对于一个更通用的项目而言是一个有趣的思路,不仅限于蜂巢布局,而是适用于各种疯狂的布局。 - Konrad Morawski
@MikeM。当然,把它放在这里,这样我也可以看一下。当然,如果你想要并且认为它已经准备好展示了。 - sandalone
@MikeM。是的。然后我就可以选择要激活或停用哪个蜂箱了。更重要的是要创建这样的布局,使其在所有类型的Android屏幕上看起来漂亮。 - sandalone
1个回答

0
这个回答有些晚了,但是可能有人会从中找到解决方案,所以我来回答这个问题。
我使用ConstraintLayout制作了一个简单的蜂窝视图。
以下是代码,您可以将ImageView替换为任何其他视图。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.MyFragment">

    <com.myapp.widget.CustomTextView
        android:id="@+id/iv_1"
        style="@style/tv_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/honey_comb_center"
        android:gravity="center"
        android:text="Total\nInvestment"
        app:fontName="@string/font_bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/iv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/_5sdp"
        android:src="@drawable/honey_comb"
        app:layout_constraintBottom_toBottomOf="@id/iv_1"
        app:layout_constraintRight_toLeftOf="@id/iv_1"
        app:layout_constraintTop_toTopOf="@id/iv_1" />

    <ImageView
        android:id="@+id/iv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_5sdp"
        android:src="@drawable/honey_comb"
        app:layout_constraintBottom_toBottomOf="@id/iv_1"
        app:layout_constraintLeft_toRightOf="@id/iv_1"
        app:layout_constraintTop_toTopOf="@id/iv_1" />

    <ImageView
        android:id="@+id/iv_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/honey_comb"
        android:translationY="@dimen/_20sdp"
        app:layout_constraintBottom_toTopOf="@id/iv_1"
        app:layout_constraintLeft_toLeftOf="@id/iv_2"
        app:layout_constraintRight_toRightOf="@id/iv_1" />

    <ImageView
        android:id="@+id/iv_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/honey_comb"
        android:translationY="@dimen/_20sdp"
        app:layout_constraintBottom_toTopOf="@id/iv_1"
        app:layout_constraintLeft_toLeftOf="@id/iv_1"
        app:layout_constraintRight_toRightOf="@id/iv_3" />

    <ImageView
        android:id="@+id/iv_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/honey_comb"
        android:translationY="@dimen/_minus20sdp"
        app:layout_constraintLeft_toLeftOf="@id/iv_2"
        app:layout_constraintRight_toRightOf="@id/iv_1"
        app:layout_constraintTop_toBottomOf="@id/iv_1" />

    <ImageView
        android:id="@+id/iv_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/honey_comb"
        android:translationY="@dimen/_minus20sdp"
        app:layout_constraintLeft_toLeftOf="@id/iv_1"
        app:layout_constraintRight_toRightOf="@id/iv_3"
        app:layout_constraintTop_toBottomOf="@id/iv_1" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是屏幕截图 在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接