android - 如何在xml android中构建梯形形状?

标签 android android-layout android-shapedrawable

我想用 bottom linetext 构建这个 shape 我有点困惑如何实现这个我累了 一些代码,但没有得到必需的东西。

desired result

到目前为止我已经尝试过这段代码

形状.xml

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

    <!-- Colored rectangle-->
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="100dp"
                android:height="40dp" />
            <solid android:color="#13a89e" />
        </shape>
    </item>


    <!-- This rectangle for the right side -->
    <!-- Their color should be the same as layout's background -->
    <item
        android:right="-100dp"
        android:left="100dp"
        android:top="-100dp"
        android:bottom="-100dp">
        <rotate
            android:fromDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#ffffff" />
            </shape>
        </rotate>
    </item>

</layer-list>

它提供了以下结果。 my attempt

我还需要这个形状下方的黄线。

感谢您的帮助。

最佳答案

这是您的 XML:

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

<!-- Colored rectangle-->
<item>
    <shape android:shape="rectangle">
        <padding android:top="35dp"/>
        <size android:width="200dp" android:height="40dp" />
        <solid android:color="#13a89e" />
    </shape>
</item>
<!-- Darker colored line-->
<item>
    <shape android:shape="line">
        <size android:width="100dp"/>
        <stroke android:width="4dp" android:color="#123456" />
    </shape>
</item>


<!-- This rectangle for the right side -->
<!-- Their color should be the same as layout's background -->
<item
    android:right="-200dp"
    android:left="200dp"
    android:top="-200dp"
    android:bottom="-200dp">
    <rotate android:fromDegrees="45">
        <shape android:shape="rectangle">
           <padding android:top="-35dp"/>
           <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>
<!-- This rectangle for the left side -->
<item
    android:right="200dp"
    android:left="-200dp"
    android:top="-200dp"
    android:bottom="-200dp">
    <rotate android:fromDegrees="-45">
        <shape android:shape="rectangle">
            <padding android:top="-35dp"/>
            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>

这就是它呈现的内容:

Result

这是我的 TextView XML:

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="io.kalabalik.tilted.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/box"
    android:text="Your Text!"
    android:textColor="#000000"
    android:gravity="center_horizontal|bottom"
    android:paddingBottom="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

关于android - 如何在xml android中构建梯形形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46501129/

相关文章:

android - 相对于另一个元素调整宽度

EditText 的 Android 自定义形状

java - 剪切 XML 形状的一部分

android - 如何以编程方式制作带笔划的圆形可绘制对象?

android - Android 设备上未显示 Firebase 通知

java - 如何以编程方式隐藏 android 一些但不是全部的 XML 输出中的查看项目?

android - PhoneGap 的 getPicture() 调用不会保存到 Android 手机上的图库

android - 如何在android中加载 Activity 的特定布局

安卓最有值(value)球员。谁关心从对话框和外部应用程序启动和获取数据?

java - 将 CardView 添加到 FrameLayout 会忽略 CardView 的 XML 文件中定义的 LayoutParams