Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開發教程之view組件添加邊框示例

android開發教程之view組件添加邊框示例

編輯:關於Android編程

給TextureView添加邊框(專業名詞為描邊),有三種解決方案:

1.設置一個9 patch 的,右邊框,中間是空的PNG。

2.自定義一個View,用Canvas畫個邊框。

3.用Android提供的ShapeDrawable來定義一個邊框。

個人比較建議采用第三種方式,原因是因為第三種只要寫XML,速度快,占用資源小,代碼編寫量也少,便於維護。

使用方法如下:

1.定義一個background.xml文件。

復制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
    android:width="2dp"
    android:color="#0000AA" />   
</shape> <span></span>

2.在View的src設置背景時使用就可以了。

復制代碼 代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:gravity="center"
        android:src="@drawable/background"
        android:padding="2dp"> 
        <TextureView         
            android:layout_width="match_parent"                             
            android:layout_height="match_parent"/>
        <com.android.camera.ui.RotateLayout
            android:layout_width="wrap_content"
            android:layout_width="wrap_content">
            <TextView
                 android:layout_width="wrap_content"
                 android:layout_width="wrap_content"
                 android:textColor="#FFFFFF"
                 android:textSize="22sp"
                 android:background="#0000000"/>
       </com.android.camera.ui.RoateLayout>
</RelativeLayout>

需要注意的是我給TextureView的父容器RelativeLayout設置了Drawable,並設置了Padding。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved