Android-如何給View新增邊框,邊框顏色
阿新 • • 發佈:2019-01-07
有時候我們會碰到自定義按鈕的操作,那麼按鈕如何自定義?
如果只是簡單的一條橫線或者豎線,直接使用TextView控制元件,寬或者高固定1dp,高或者寬match parent,在定義一個background="#FF0000",這樣就實現了單一的線條功能。線條的顏色就是指定的背景顏色,線粗就是寬或者高。
自己製作一個shape佈局,在需要使用的地方通過background屬性引用即可。
二、關鍵程式碼
1.shape_button.xml
- <?xmlversion="1.0"encoding="utf-8"?>
-
<shapexmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <stroke
- android:width="1dp"
- android:color="#ebebeb"/>
- <padding
- android:bottom="1dp"
- android:left="1dp"
- android:right="1dp"
- android:top="1dp"/>
- <solidandroid:color="#00000000"/>
- </shape>
2.引用的佈局檔案
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="120dp"
- android:layout_marginLeft="2dp"
- android:layout_marginRight="2dp"
-
android:background="@drawable/shape_textview_cart"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/cart_image"
- android:layout_width="120dp"
- android:layout_height="120dp"
- android:layout_marginRight="1dp"
- android:src="@drawable/qzone"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <includelayout="@layout/include_cart_listview_item_right_01"/>
- <includelayout="@layout/include_cart_price_number"/>
- </LinearLayout>
- </LinearLayout>