1. 程式人生 > >Android-如何給View新增邊框,邊框顏色

Android-如何給View新增邊框,邊框顏色

有時候我們會碰到自定義按鈕的操作,那麼按鈕如何自定義?

如果只是簡單的一條橫線或者豎線,直接使用TextView控制元件,寬或者高固定1dp,高或者寬match parent,在定義一個background="#FF0000",這樣就實現了單一的線條功能。線條的顏色就是指定的背景顏色,線粗就是寬或者高。

自己製作一個shape佈局,在需要使用的地方通過background屬性引用即可。

二、關鍵程式碼

1.shape_button.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:shape="rectangle">
  4.     <stroke
  5.         android:width="1dp"
  6.         android:color="#ebebeb"/>
  7.     <padding
  8.         android:bottom="1dp"
  9.         android:left="1dp"
  10.         android:right="1dp"
  11.         android:top="1dp"/>
  12.     <solidandroid:color="#00000000"/>
  13. </shape>

2.引用的佈局檔案
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="120dp"
  5.     android:layout_marginLeft="2dp"
  6.     android:layout_marginRight="2dp"
  7.     android:background="@drawable/shape_textview_cart"
  8.     android:orientation="horizontal">
  9.     <ImageView
  10.         android:id="@+id/cart_image"
  11.         android:layout_width="120dp"
  12.         android:layout_height="120dp"
  13.         android:layout_marginRight="1dp"
  14.         android:src="@drawable/qzone"/>
  15.     <LinearLayout
  16.         android:layout_width="match_parent"
  17.         android:layout_height="match_parent"
  18.         android:orientation="vertical">
  19.         <includelayout="@layout/include_cart_listview_item_right_01"/>
  20.         <includelayout="@layout/include_cart_price_number"/>
  21.     </LinearLayout>
  22. </LinearLayout>