[英]Android: how to add a triangle shape in the right-top button corner
I would like to do this kind of button with a triangle in the right-top corner of a layout : 我想在布局的右上角做一个带三角形的按钮:
I've started this layout without this triangle yet: 我已经开始没有这个三角形的布局了:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="@dimen/keyline_1"
android:paddingRight="@dimen/keyline_2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/grey"
android:text="DESCRIPTION"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="@dimen/keyline_4"
android:textColor="@color/grey"
android:text="info"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/grey"
android:text="TITLE"/>
</LinearLayout>
</RelativeLayout>
Use below code to create triangle shape and make it textview background 使用下面的代码创建三角形形状并使其成为textview背景
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="-45"
android:toDegrees="45"
android:pivotX="0%"
android:pivotY="1%" >
<shape android:shape="rectangle" >
<stroke
android:width="10dp"
android:color="#00000000" />
<solid android:color="#00ACED" />
</shape>
</rotate>
</item>
</layer-list>
and use below code to rotate the textview 并使用下面的代码来旋转textview
<TextView
android:id="@+id/won_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingTop="20dp"
android:rotation="-45"
android:text="@string/won"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="34sp" />
more detail refer How to make custom textview in android? 更多细节参考如何在android中制作自定义textview?
You've to create a shape
and add it as a background
like this one : 你要创建一个shape
并将其添加为像这样的background
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="@color/transparent" android:width="10dp"/>
<solid
android:color="@color/your_color_here" />
</shape>
</rotate>
</item>
Add a TextView
and put this shape
as a background
and change the gravity
to end
添加TextView
并将此shape
作为background
并将gravity
更改为end
Example : 示例:
android:background:"@drawable/YOUR_SHAPE"
Also if you don't familiar with shapes
try this answer 此外,如果您不熟悉shapes
尝试此答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.