如何在 Android XML 中自动镜像形状

Moh*_*air 3 android

我有一张自定义卡片,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="?attr/colorListItemBackground" />

    <corners
        android:bottomLeftRadius="16dp"
        android:topLeftRadius="16dp" />

</shape>
Run Code Online (Sandbox Code Playgroud)

我的应用程序也支持 RTL 语言,因此更改为 RTL,此形状不会更改(自动镜像)。

我可以做哪些更改才能使其成为自动后视镜?

注意圆附近的圆角。 LTR_Image

圆角仍然在同一个地方。 RTL_Image

Sum*_*hoo 5

您可以在里面定义自定义形状

res/
  drawable 
  drawable-ldltr 
  drawable-ldrtl 
Run Code Online (Sandbox Code Playgroud)

现在把镜像的自定义形状放在里面drawable-ldrtl,它应该可以工作。

您的自定义形状 xml 应该完全相反,即

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="?attr/colorListItemBackground" />

    <corners
        android:bottomRightRadius="16dp"
        android:topRightRadius="16dp" />

</shape>
Run Code Online (Sandbox Code Playgroud)

您也可以添加android:autoMirrored="true"到您的矢量 drawable 中,它应该自动镜像它。但这需要您根据文档拥有矢量图像:https : //developer.android.com/reference/android/graphics/drawable/VectorDrawable

因此,一个快捷方式是用矢量可绘制对象环绕您的形状

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/your_shape"
        android:autoMirrored="true"/>
Run Code Online (Sandbox Code Playgroud)

让我知道这是否适合您。