【问题标题】:How Can I Invert a Border In XML Shape?如何在 XML 形状中反转边框?
【发布时间】:2021-10-06 09:37:58
【问题描述】:

我正在尝试在 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>

在研究方面,我看过一些例子,例如this,但尝试添加其他形状

不过,我正在寻找一些关于如何更好地制作它的建议,因为我认为固定尺寸和尺寸行不通。在我上面的屏幕截图中,绿色背景文本的宽度是可变的,所以理想情况下,我需要一个能够保持其右上角倒置边框的形状,但具有动态调整大小的宽度。

如果有人能指出我右上方倒边框的正确方向,我将不胜感激。谢谢

【问题讨论】:

标签: android xml android-layout shapes


【解决方案1】:

这看起来像是9-patch drawable 的一个很好的应用程序。有一点学习曲线,但 9-patch drawables 可能很有用。

我为您截取了包含文本的部分的图像,并将其加载到 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 Patch 2)无法正确显示此 9-patch,但它确实显示在模拟器上。

【讨论】:

  • 非常感谢,所以澄清一下,我应该在左边设置一个文本视图(带有绿色背景和文本),然后设置第二个文本视图,其中有一个绿色倒角约束在它旁边?
  • @trainmania100 我不明白你的评论。布局是两个独立的 TextViews,具有相同的 9-patch drawable 作为背景。 9-patch 文件名为 sample.9.png.
  • 谢谢你的帖子看起来不错,我会标记为答案,我不熟悉 9 补丁所以会做一些研究。谢谢你
猜你喜欢
  • 1970-01-01
  • 2014-10-07
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多