自定義文本背景,圓角邊框

1、創建基礎的xml佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="帶邊框的文本"
        android:textSize="24pt"
        android:background="@drawable/bg_border"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="圓角邊框"
        android:textSize="24pt"
        android:background="@drawable/bg_border2"/>

</LinearLayout>
2、自定義背景bg_border
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--設置背景爲透明色-->
    <solid android:color="#0000"/>
    <stroke android:width="4px"
            android:color="#f00"/>
</shape>

自定義背景bg_border2

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--圓角矩形的四個圓角-->
    <corners android:topLeftRadius="20px"
             android:topRightRadius="5px"
             android:bottomRightRadius="20px"
             android:bottomLeftRadius="5px"/>
    <!--指定邊框的寬度和顏色-->
    <stroke android:width="4px" android:color="#f0f"/>
    <!--指定使用漸變背景色,使用sweep類型漸變,顏色從紅色-》綠色-》藍色-->
    <gradient android:startColor="#f00"
              android:centerColor="#0f0"
              android:endColor="#00f"
              android:type="sweep"/>
</shape>
3、加載顯示的Activity
public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_circle);
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章