2017-07-04 345 views

回答

0

如果它只是形状也许矢量绘制就足够了,但绘制形状,图标和文字,更好地创建自己的Button类和借鉴的东西重写onDraw。该形状可以使用Path来绘制。

1

你可以通过你的绘制

background.xml内创建一个可绘制的XML文件,这样做

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="27dp" 
    android:height="24dp" 
    android:viewportHeight="628.0" 
    android:viewportWidth="726.0"> 
    <path 
     android:fillColor="#00ffffff" 
     android:pathData="m723,314c-60,103.9 -120,207.8 -180,311.8 -120,0 -240,0 -360,0C123,521.8 63,417.9 3,314 63,210.1 123,106.2 183,2.2c120,0 240,0 360,0C603,106.2 663,210.1 723,314Z" 
     android:strokeColor="#ffffff" 
     android:strokeWidth="20" /> 
</vector> 

然后在您的布局文件,你可以添加背景像下面

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#652ef0" 
    android:orientation="vertical" 
    tools:context="com.MainActivity"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/background" 
     android:gravity="center" 
     android:text="Hello World!" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/background" 
     android:gravity="center" 
     android:text="Hello World 2!" /> 
</LinearLayout> 

这会给你的输出是这样的 activity_main