如何创建一个倾斜的按钮?

时间:2018-07-21 12:15:35

标签: android button

我正在寻找如何创建如图所示的倾斜按钮:

enter image description here

我尝试了一些解决方案:How to create parallelogram shape background?

Android: how to create shape with skewed two-tone color? 但是我找不到我想要的东西

1 个答案:

答案 0 :(得分:1)

有一个第三方库,可以给出倾斜的Textview形状。您可以将侦听器设置为Textview并执行所需的功能。您可以通过更改app:slantedMode =“ left”的值来更改SlantedTextView的方向,也可以通过更改app:slantedLength =“ 55dp”的值来增加和减少SlantedTextView的大小。

库:

compile 'com.haozhang.libary:android-slanted-textview:1.2'

链接: https://github.com/HeZaiJin/SlantedTextView`

XML代码:

<com.haozhang.lib.SlantedTextView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="96dp"
    android:layout_marginTop="139dp"
    android:gravity="center"
    app:slantedBackgroundColor="@color/colorPrimaryDark"
    app:slantedLength="55dp"
    app:slantedMode="left"
    app:slantedText="IOS"
    app:slantedTextColor="@color/colorPrimary"
    app:slantedTextSize="16sp" />

Java代码:

SlantedTextView stv = (SlantedTextView) findViewById(R.id.test);

stv.setText("PHP")
        .setTextColor(Color.WHITE)
        .setSlantedBackgroundColor(Color.BLACK)
        .setTextSize(18)
        .setSlantedLength(50)
        .setMode(SlantedTextView.MODE_LEFT);

enter image description here

如果您想要类似的东西,那么我已经在drawable中编写了一些代码。可能可以帮助您

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="43dp"
        android:left="43dp"
        android:right="43dp"
        android:top="43dp">

        <rotate android:fromDegrees="10">

            <shape android:shape="rectangle">
                <size
                    android:width="200dp"
                    android:height="200dp" />
                <gradient
                    android:endColor="#ecf566"
                    android:startColor="#00000000" />
                <stroke
                    android:width="2dp"
                    android:color="#1414d2" />
            </shape>
        </rotate>
    </item>
</layer-list>

enter image description here

相关问题