如何反转 XML 形状的边框?
How Can I Invert a Border In XML Shape?
我正在尝试在 Android XML (Kotlin) 中获得一个矩形形状,使其具有倒置的右上边框。通过这个,我的意思是而不是弯曲的,圆形的右上角边界向左移动,我希望它向右舍入,就像一个尖点,然后返回到左上角。
为了解释我的意思,请在下面找到一些屏幕截图。
到目前为止我得到了什么
我的目标是什么
就目前的代码而言,使用 Stack Overflow 上其他地方的一些示例,我生成了以下代码。这是一个非常简单的白色形状,带有左上半径的边框,位于绿色背景形状的顶部,绿色形状的右侧。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="@color/rsMoving"/>
</shape>
</item>
<item android:left="200dp" android:right="30dp" >
<shape android:shape="rectangle">
<solid android:color="@color/rsMoving" />
</shape>
</item>
<item android:left="200dp" android:right="0dp" >
<shape android:shape="rectangle">
<corners
android:topLeftRadius="50dp"/>
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
在研究方面,我看过一些示例,例如 ,但尝试添加其他形状
但是我正在寻找一些关于如何更好地制作它的建议,因为我认为固定的尺寸和尺寸不会奏效。在我上面的屏幕截图中,绿色背景文本的宽度是可变的,所以理想情况下我需要一个形状来保持其右上角的倒置边框,但宽度可以动态调整。
如果有人能指出右上角倒边框的正确方向,我将不胜感激。谢谢
这看起来像是 9-patch drawable 的一个很好的应用。有一点学习曲线,但 9 补丁可绘制文件可能很有用。
我给你拍了一张图片,把有文字的部分切下来,然后加载到 Android Studio 应用补丁。
左侧和顶部的黑条标识可以扩展以增加可绘制对象的区域。右侧和底部的栏标识可以放置文本的位置。使用这个 XML:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:backgroundTint="@android:color/darker_gray">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/sample"
android:text="This is some wider text!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/sample"
android:text="This is some text!"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
</androidx.constraintlayout.widget.ConstraintLayout>
我们看到以下内容
Android Studio(Arctic Fox 2020.3.1 补丁 2)未正确显示此 9 补丁,但它在模拟器上确实显示为此处所示。
我正在尝试在 Android XML (Kotlin) 中获得一个矩形形状,使其具有倒置的右上边框。通过这个,我的意思是而不是弯曲的,圆形的右上角边界向左移动,我希望它向右舍入,就像一个尖点,然后返回到左上角。
为了解释我的意思,请在下面找到一些屏幕截图。
到目前为止我得到了什么
我的目标是什么
就目前的代码而言,使用 Stack Overflow 上其他地方的一些示例,我生成了以下代码。这是一个非常简单的白色形状,带有左上半径的边框,位于绿色背景形状的顶部,绿色形状的右侧。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="@color/rsMoving"/>
</shape>
</item>
<item android:left="200dp" android:right="30dp" >
<shape android:shape="rectangle">
<solid android:color="@color/rsMoving" />
</shape>
</item>
<item android:left="200dp" android:right="0dp" >
<shape android:shape="rectangle">
<corners
android:topLeftRadius="50dp"/>
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
在研究方面,我看过一些示例,例如
但是我正在寻找一些关于如何更好地制作它的建议,因为我认为固定的尺寸和尺寸不会奏效。在我上面的屏幕截图中,绿色背景文本的宽度是可变的,所以理想情况下我需要一个形状来保持其右上角的倒置边框,但宽度可以动态调整。
如果有人能指出右上角倒边框的正确方向,我将不胜感激。谢谢
这看起来像是 9-patch drawable 的一个很好的应用。有一点学习曲线,但 9 补丁可绘制文件可能很有用。
我给你拍了一张图片,把有文字的部分切下来,然后加载到 Android Studio 应用补丁。
左侧和顶部的黑条标识可以扩展以增加可绘制对象的区域。右侧和底部的栏标识可以放置文本的位置。使用这个 XML:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:backgroundTint="@android:color/darker_gray">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/sample"
android:text="This is some wider text!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/sample"
android:text="This is some text!"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
</androidx.constraintlayout.widget.ConstraintLayout>
我们看到以下内容
Android Studio(Arctic Fox 2020.3.1 补丁 2)未正确显示此 9 补丁,但它在模拟器上确实显示为此处所示。