微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:如何在右上角的按钮角添加三角形

我想在布局的右上角做一个带三角形的按钮:

我已经开始没有这个三角形的布局了:

<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>

解决方法

使用下面的代码创建三角形形状并使其成为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>

并使用下面的代码来旋转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" />

更多细节参见How to make custom textview in android?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐