如何在Android中创建一个带有居中圆圈形状和图标的矩形?

问题描述 投票:0回答:1

我正在尝试在BottomNavigationView(BottomSheetDialog peek)上创建一个可见的形状。有关如何在应用程序中重新创建它的任何想法?

Intended effect

我一直在尝试使用图层列表,它在预览中看起来很好,但在将其设置为视图的背景时会变平。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-13dp" android:start="120dp" android:end="120dp">
        <shape android:shape="oval">
            <solid android:color="@color/turquoise"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/turquoise"/>
        </shape>
    </item>
    <item android:drawable="@drawable/ic_bottomsheetdialog_arrow_up"
          android:bottom="15dp"
          android:start="130dp"
          android:end="130dp"
          android:top="-10dp"/>
</layer-list>

XML预览:

XML Result in preview

Drawable应用于布局中的View

   <View android:id="@+id/view_bottomSheetDialog_accentDivider"
      android:layout_width="0dp"
      android:background="@drawable/drawable_bottom_sheet_dialog"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.0"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      android:layout_height="30dp"/>

enter image description here

我也一直在考虑使用带有居中的FloatingActionButton的BottomNavigationView等变通方法,或只考虑以ConstraintLayout为中心的2个视图(带有图标的矩形+圆形)。

有关如何执行此操作或代码的任何指示都非常有用。

android xml shapes
1个回答
1
投票

既然我已经弄明白了,我会在这里发布我的解决方案

我删除了负边距,在矩形中添加了一些正面,并使用了带有android:src“@drawable ...”的ImageView而不是原始视图

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:end="200dp"
        android:gravity="center_horizontal"
        android:start="200dp">
        <shape android:shape="oval">
            <size
                android:width="48dp"
                android:height="@dimen/bottomdialog_peek_height" />
            <solid android:color="@color/turquoise" />
        </shape>
    </item>
    <item android:top="10dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/turquoise" />
        </shape>
    </item>
    <item
        android:drawable="@drawable/ic_bottomsheetdialog_arrow_up"
        android:gravity="center_horizontal" />

</layer-list>

这是ImageView:

 <ImageView
    android:id="@+id/imageView_bottomSheetDialog_peek"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/drawable_bottom_sheet_dialog"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:contentDescription="@string/bottom_sheet_dialog_peek" />
© www.soinside.com 2019 - 2024. All rights reserved.