简体   繁体   English

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

[英]How to show xml shape in background at specific place android

I have XML of the light brown circle and I want to place it as background on the top left corner as shown in the pic我有浅棕色圆圈的 XML,我想把它作为背景放在左上角,如图所示

在此处输入图像描述

Can anyone guide me on how I can do it?谁能指导我如何做到这一点? I need to show it on multiple screens on different sides.我需要在不同侧面的多个屏幕上显示它。 Any hint will be much appreciated任何提示将不胜感激

here is the XML of circle这里是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>

You can wrap your ImageView in ConstraintLayout and add constraints of:您可以将ImageView包装在ConstraintLayout中并添加以下约束:

  • app:layout_constraintDimensionRatio="1:1" : Making width and height equal (or as proportional as you want) app:layout_constraintDimensionRatio="1:1" :使宽度和高度相等(或按您想要的比例)

  • app:layout_constraintWidth_percent="0.5" : a percentage you need of the Width relative to the parent ConstraintLayout , here I see it half app:layout_constraintWidth_percent="0.5" : 你需要的 Width 相对于父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>

You can use the following quarter of a circle drawable instead of a full circle您可以使用可绘制的以下四分之一圆而不是整个圆

drawable\quarter.xml可绘制\季度。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>

Result:结果:

在此处输入图像描述

You can just take different images with that quarter circle placed in different places as per your requirement and set the image as a background of your layout.您可以根据需要将四分之一圆放在不同的位置拍摄不同的图像,并将图像设置为布局的背景。

You can use ConstraintLayout or FrameLayout as your parent layout.您可以使用ConstraintLayoutFrameLayout作为父布局。 Here's an example with ConstraintLayout .这是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM