按钮仅有2个角

时间:2017-12-22 21:26:17

标签: android

我必须创建如下样式的按钮:

enter image description here

是的,奇怪我知道。如果按钮文本更短/更长,则这两个角不应缩放。

是否可以使用XML创建它?

  • 我尝试了一个矢量,但矢量按照按钮大小缩放。
  • 我所拥有的任何其他想法都是以编程方式进行的,例如this answer
  • 中的内容

(设计说明:我们正在尝试设计。想象一下按钮有4个这样的角落。按钮彼此相邻,每个都有2个角靠近另一个,等等。我们的用户喜欢花哨的设计.. ...... :-))

2 个答案:

答案 0 :(得分:1)

您可以创建customView

 <LinearLayout
    android:orientation="vertical"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="100dp"
    android:layout_width="100dp"
    android:layout_height="wrap_content">
    <View
        android:layout_marginLeft="90dp"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:gravity="center"
        android:textSize="13dp"
        android:textColor="#000000"
        android:text="Button text.."
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp" />
        <RelativeLayout
            android:layout_width="1dp"
            android:layout_height="match_parent">
       <View
           android:background="#000000"
           android:layout_width="1dp"
           android:layout_height="10dp"/>
        <View
            android:layout_alignParentBottom="true"
            android:background="#000000"
            android:layout_width="1dp"
            android:layout_height="10dp"/>
        </RelativeLayout>
    </LinearLayout>
    <View
        android:layout_marginLeft="90dp"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
</LinearLayout>

image like this

答案 1 :(得分:1)

好的经过一些研究和最佳实践后,您肯定需要Layerlist作为Button的背景(尽管即使TextView也可以,但它也可以像任何视图一样点击)

<强> SOLUTION:

您必须打开drawables文件夹并添加名为let custom_button_background的可绘制资源,然后在其中使用此图层列表:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#000000" />
        </shape>
    </item>
    <item android:top="20dp" android:bottom="20dp" android:left="20dp" android:right="20dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item android:right="80dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item android:top="80dp" android:bottom="80dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
</layer-list>

然后我们完成了!这将是我们的背景,在我的Android工作室中,此预览看起来像这样:
image on how it looks
您可以调整值以根据需要减少它们。要将其设置为Button或任何View,只需将此属性添加到其中:

android:background="@drawable/custom_button_background"

只需调整尺寸以适合您的按钮尺寸!