Example of adding a border to the view component in the android development tutorial

  • 2020-05-27 07:09:32
  • OfStack

There are three solutions for adding a border (stroke in the technical sense) to TextureView:

1. Set 1 9 patch, right border, with empty PNG in the middle.

2. Customize 1 View and draw a border with Canvas.

3. Define a border using the ShapeDrawable provided by Android.

Compared with others, I suggest using the third method, because the third method only needs to write XML, which is fast, occupies little resources, and is easy to maintain.

Usage:

1. Define one background.xml file.


<?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. Just use View's src to set the background.


<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>

Note that I set Drawable for RelativeLayout, the parent container of TextureView, and Padding.


Related articles: