如何在特定位置 android 的背景中显示 xml 形状

How to show xml shape in background at specific place android

我有 XML 个浅棕色圆圈,我想把它作为背景放在左上角,如图所示

任何人都可以指导我如何做吗?我需要在不同侧面的多个屏幕上显示它。任何提示将不胜感激

这里是 circle

的 XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/light_brown"/>
            <!-- Set the same value for both width and height to get a circular shape -->
            <size android:width="550sp" android:height="550sp"/>
        </shape>
    </item>
</selector>

您可以根据需要在不同的位置拍摄不同的图像,并将该图像设置为布局的背景。

您可以使用 ConstraintLayoutFrameLayout 作为父布局。这是 ConstraintLayout.

的示例
<android.support.constraint.ConstraintLayout 
  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"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bg_image"
        android:scaleType="fitXY"/>

</android.support.constraint.ConstraintLayout>

您可以将 ImageView 包裹在 ConstraintLayout 中并添加约束条件:

  • app:layout_constraintDimensionRatio="1:1": 使宽度和高度相等(或按你想要的比例)

  • app:layout_constraintWidth_percent="0.5":你需要的宽度相对于父级的百分比ConstraintLayout,这里我看到一半

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/profile_image_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/quarter"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

您可以使用可绘制的四分之一圆代替完整的圆

drawable\quarter.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="15dp"
                android:height="15dp" />
            <solid android:color="@color/light_brown" />
            <corners android:bottomRightRadius="250dp" />
        </shape>
    </item>
</layer-list>

结果: