W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
學習完Android中的六大布局,從本節(jié)開始我們來一個個講解Android中的UI控件,本節(jié)給大家?guī)淼腢I控件是:TextView(文本框),用于顯示文本的一個控件,另外聲明一點,我不是翻譯API文檔,不會一個個屬性的去扣,只學實際開發(fā)中常用的,有用的,大家遇到感覺到陌生的屬性可以查詢對應的API!當然,每一節(jié)開始都會貼這一節(jié)對應API文檔的鏈接:TextView API 好了,在開始本節(jié)內容前,先要介紹下幾個單位:
dp(dip): device independent pixels(設備獨立像素). 不同設備有不同的顯示效果,這個和設備硬件有關,一般我們?yōu)榱酥С諻VGA、HVGA和QVGA 推薦使用這個,不依賴像素。 px: pixels(像素). 不同設備顯示效果相同,一般我們HVGA代表320x480像素,這個用的比較多。 pt: point,是一個標準的長度單位,1pt=1/72英寸,用于印刷業(yè),非常簡單易用; sp: scaled pixels(放大像素). 主要用于字體顯示best for textsize。
通過下面這個簡單的界面,我們來了解幾個最基本的屬性:
布局代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:background="#8fffad">
<TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:text="TextView(顯示框)"
android:textColor="#EA5246"
android:textStyle="bold|italic"
android:background="#000000"
android:textSize="18sp" />
</RelativeLayout>
上面的TextView中有下述幾個屬性:
background:控件的背景顏色,可以理解為填充整個控件的顏色,可以是圖片哦!
涉及到的幾個屬性:
效果圖:
實現代碼:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:shadowColor="#F9F900"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:shadowRadius="3.0"
android:text="帶陰影的TextView"
android:textColor="#4A4AFF"
android:textSize="30sp" />
如果你想為TextView設置一個邊框背景,普通矩形邊框或者圓角邊框!下面可能幫到你! 另外TextView是很多其他控件的父類,比如Button,也可以設置這樣的邊框! 實現原理很簡單,自行編寫一個ShapeDrawable的資源文件!然后TextView將blackgroung 設置為這個drawable資源即可!
簡單說下shapeDrawable資源文件的幾個節(jié)點以及屬性:
- 這個是設置背景顏色的
- 這個是設置邊框的粗細,以及邊框顏色的
- 這個是設置邊距的
- 這個是設置圓角的
- 這個是設置漸變色的,可選屬性有: startColor:起始顏色 endColor:結束顏色 centerColor:中間顏色 angle:方向角度,等于0時,從左到右,然后逆時針方向轉,當angle = 90度時從下往上 type:設置漸變的類型
實現效果圖:
代碼實現:
Step 1:編寫矩形邊框的Drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 設置一個黑色邊框 -->
<stroke android:width="2px" android:color="#000000"/>
<!-- 漸變 -->
<gradient
android:angle="270"
android:endColor="#C0C0C0"
android:startColor="#FCD209" />
<!-- 設置一下邊距,讓空間大一點 -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
</shape>
Step 2:編寫圓角矩形邊框的Drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 設置透明背景色 -->
<solid android:color="#87CEEB" />
<!-- 設置一個黑色邊框 -->
<stroke
android:width="2px"
android:color="#000000" />
<!-- 設置四個圓角的半徑 -->
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<!-- 設置一下邊距,讓空間大一點 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
Step 3:將TextView的blackground屬性設置成上面這兩個Drawable:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="64dp"
android:textSize="18sp"
android:gravity="center"
android:background="@drawable/txt_rectborder"
android:text="矩形邊框的TextView" />
<TextView
android:id="@+id/txtTwo"
android:layout_width="200dp"
android:layout_height="64dp"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:gravity="center"
android:background="@drawable/txt_radiuborder"
android:text="圓角邊框的TextView" />
</LinearLayout>
在實際開發(fā)中,我們可能會遇到這種需求:
如圖,要實現這種效果,可能你的想法是:一個ImageView用于顯示圖片 + 一個TextView用于顯示文字,然后把他們丟到一個LinearLayout中,接著依次創(chuàng)建四個這樣的小布局,再另外放到一個大的LinearLayout中,效果是可以實現,但是會不會有點繁瑣呢?而且前面我們前面也說過,布局層次越少,性能越好!使用drawableXxx就可以省掉上面的過程,直接設置四個TextView就可以完成我們的需求!
基本用法:
設置圖片的核心其實就是:drawableXxx;可以設置四個方向的圖片:drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右) 另外,你也可以使用drawablePadding來設置圖片與文字間的間距!
效果圖:(設置四個方向上的圖片)
實現代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jay.example.test.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableTop="@drawable/show1"
android:drawableLeft="@drawable/show1"
android:drawableRight="@drawable/show1"
android:drawableBottom="@drawable/show1"
android:drawablePadding="10dp"
android:text="張全蛋" />
</RelativeLayout>
一些問題: 可能你會發(fā)現,我們這樣設置的drawable并不能自行設置大小,在XML是無法直接設置的; 所以我們需要在Java代碼中來進行一個修改!
示例代碼如下:
package com.jay.example.test;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtZQD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtZQD = (TextView) findViewById(R.id.txtZQD);
Drawable[] drawable = txtZQD.getCompoundDrawables();
// 數組下表0~3,依次是:左上右下
drawable[1].setBounds(100, 0, 200, 200);
txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],
drawable[3]);
}
}
運行效果圖:
代碼分析:
- ①Drawable[] drawable = txtZQD.getCompoundDrawables( ); 獲得四個不同方向上的圖片資源,數組元素依次是:左上右下的圖片
- ②drawable[1].setBounds(100, 0, 200, 200); 接著獲得資源后,可以調用setBounds設置左上右下坐標點,比如這里設置了代表的是: 長是:從離文字最左邊開始100dp處到200dp處 寬是:從文字上方0dp處往上延伸200dp!
- ③txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);為TextView重新設置drawable數組!沒有圖片可以用null代替哦! PS:另外,從上面看出我們也可以直接在Java代碼中調用setCompoundDrawables為 TextView設置圖片!
當文字中出現了URL,E-Mail,電話號碼,地圖的時候,我們可以通過設置autoLink屬性;當我們點擊 文字中對應部分的文字,即可跳轉至某默認APP,比如一串號碼,點擊后跳轉至撥號界面!
看下效果圖:
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: