今天主要介紹的UI組件為:
TextView
會演示TextView怎樣將文本帶上邊框
與超文本鏈接
TextView
自定義帶邊框的TextView
1.activity文件
- package cn.class3g.activity;
-
- import Android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.TextView;
-
- public class MyBorderTextView extends TextView{
-
- //必須實現帶兩個參數的構造
- public MyBorderTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- //覆蓋父類的onDraw方法
- public void onDraw(Canvas canvas){
- super.onDraw(canvas);
-
- //創建畫刷
- Paint paint = new Paint();
- //設置顏色
- paint.setColor(android.graphics.Color.GREEN);
- //開畫
- canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);//左邊框
- canvas.drawLine(0, 0,0,this.getHeight()-1, paint);//上邊框
- canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);//右邊框
- canvas.drawLine(0,this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);//下邊框
- //canvas.drawLine(開始橫坐標,開始縱坐標,結束橫坐標,結束縱坐標)
-
- }
- }
2.layout文件
- <cn.class3g.activity.MyBorderTextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="30dp"
- android:layout_margin="10dp"
- android:textColor="#cccccc"
- android:text="一雨落塵"
- />
Layout文件中定義了內外邊距,文本內容,顏色
顯示效果:
超文本鏈接
1、Activity文件
- package cn.class3g.activity;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.Html;
- import android.widget.TextView;
-
- public class TextViewTestActivity extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.auto_link);
- //Html.fromHtml方法
- TextView tv = (TextView) this.findViewById(R.id.tvHtml);
-
- String htmlStr = "<font color='#00FF22'>我愛北京天安門</font>" +//改變字體顏色的一種方法
- "<a href='http://www.ifeng.com'>小橋流水</a>";//具有超鏈接外觀,但是不能跳轉
- tv.setText(Html.fromHtml(htmlStr));
- }
- }
注:Html.fromHtml()方法的查連接徒具其形,但不能跳轉