我想用shape
和bottom line
构建这个text
我感到很困惑如何实现这一点我累了
一些代码,但没有得到必要的东西。
到目前为止,我已尝试过此代码
shape.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>
我在这个形状下面也需要黄线。
感谢您的帮助。
答案 0 :(得分:8)
这是你的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>
这就是它所呈现的:
这是我的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>
答案 1 :(得分:2)
我强烈建议您创建自定义文本视图,以便更灵活和受控制。您需要创建路径对象,并使用该对象定义视角。如果你想要视图作为文本你需要覆盖 onDraw (Canvas canvas)函数,你将调用canvas.draw(path,paint)方法。如果您不仅需要文本字段,则应覆盖任何视图组类,但对于视图组,您应覆盖 onDispatchDraw 函数来执行此操作。
您可以像下面的示例一样创建形状
// you can define all points
Point topLeft = new Point(0,0);
Point topRight = new Point(getWidth(),0); // your view width
//...
//cover your corner points
Path path = new Path();
path.moveTo(topLeft.x, topLeft.y);
path.lineTo(topRight.x, topRight.y);
path.lineTo(bottomRight.x, bottomRight.y);
path.lineTo(shapeBottomRight.x, shapeBottomRight.y);
path.lineTo(shapeTop.x, shapeTop.y);
path.lineTo(shapeBottomLeft.x, shapeBottomLeft.y);
path.lineTo(bottomLeft.x, bottomLeft.y);
path.lineTo(topLeft.x, topLeft.y);
canvas.draw(path, paint);
答案 2 :(得分:0)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="28dp"
android:height="5dp"
android:viewportWidth="800"
android:viewportHeight="304">
<path
android:pathData="M215,41.4c0,0.2 -22.9,58.3 -51,129.1 -28,70.8 -51,129.8 -51,131.1l0,2.4 296.1,-0c281.6,-0 296.1,-0.1 295.6,-1.8 -0.2,-0.9 -11.9,-31 -25.9,-66.7 -14.1,-35.8 -36.9,-94.1 -50.9,-129.8l-25.3,-64.7 -193.8,-0c-106.6,-0 -193.8,0.2 -193.8,0.4z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="50"
android:endY="99"
android:startX="50"
android:startY="1"
android:type="linear">
<item
android:color="#FFFFFF"
android:offset="0.0" />
<item
android:color="#AAA8A8"
android:offset="0.8" />
<item
android:color="#AAA8A8"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
答案 3 :(得分:0)
这篇文章对创建梯形视图https://arkapp.medium.com/trapezium-view-for-android-584799c7e849
有很大帮助<com.arkapp.trapeziumview.TrapeziumCustomView
android:id="@+id/trapeziumCustomView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_margin="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeColor="@color/teal_200"
app:edgeRadius="18"
app:slopeLength="88"
app:slopeType="bottomRight" />
借助此功能,我可以创建一个边缘为坡度的自定义视图