【问题标题】:How to show xml shape in background at specific place android如何在特定位置android的背景中显示xml形状
【发布时间】:2020-12-29 06:42:41
【问题描述】:

我有浅棕色圆圈的 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>

【问题讨论】:

  • 如果您已经准备好 xml,只需将其添加到视图的背景中。这有什么问题?
  • @ADM 我有完整圆圈的 xml,但我应该在左上角显示一半圆圈,如图所示

标签: android android-xml android-drawable


【解决方案1】:

您可以将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>

结果:

【讨论】:

  • 它在左上角显示完整的圆圈。在设计选项卡中,它在layout_constraintDimensionRatio 上显示错误,表示浮动无效
  • @ZohabAli 感谢您的评论...对于无效的浮动,请使用 app:layout_constraintDimensionRatio="1" 代替检查 here
  • 对于圆圈...这取决于您的可绘制对象...您有一个圆圈还是四分之一?可以分享一下吗
  • 我已经更新了我的问题,您可以查看我用于圈子的 XML
  • 谢谢,它适用于这个特定的场景。但我也必须在其他屏幕上显示圆圈,但在不同的地方,所以如果我遵循这种方法,我将不得不为每个屏幕创建圆圈。有什么通用的方法可以让我在屏幕的任何特定区域显示半圈吗?
【解决方案2】:

您可以根据需要将四分之一圆放在不同的位置拍摄不同的图像,并将图像设置为布局的背景。

您可以使用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>

【讨论】:

    猜你喜欢
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    相关资源
    最近更新 更多