Android文本與視圖基本操作梳理介紹
目錄文件說明
mainifests子目錄:下面的AndroidMainifest.xml文件是APP的運(yùn)行配置文件。
java子目錄:下面有三個(gè)com.example.myapp包,第一個(gè)包存放當(dāng)前模塊的java源代碼,后面兩個(gè)包存放測試用的java代碼。
res子目錄:存放當(dāng)前模塊的資源文件,有四個(gè)子目錄:
- drawable目錄存放圖形描述文件與圖片文件
- layout目錄存放app頁面的布局文件
- mipmap存放app的啟動圖標(biāo)
- values存放一些常量定義文件,如字符串常量string.xml等。

一、設(shè)置文本內(nèi)容
有兩種方式:在XML文件中設(shè)置和在java類中調(diào)用settext方法。
在XML文件中通過屬性android:text設(shè)置
1、在layout文件下新建一個(gè)xml文件


新建后會有以下默認(rèn)內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>2、配置XML文件設(shè)置文本
在LinearLayout標(biāo)簽里添加以下內(nèi)容;
text標(biāo)簽里不寫具體的內(nèi)容是因?yàn)椋?/p>
情況:多個(gè)文件都用了相同的內(nèi)容,但是需要修改此內(nèi)容,那么就需要修改多個(gè)文件,而把內(nèi)容寫在string文件里,則只需要修改string文件的內(nèi)容。
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"/> //調(diào)用string文件里name為hello的文本3、string文件內(nèi)容
<resources>
<string name="app_name">test</string>
<string name="hello">你好</string>
</resources>
4、java類調(diào)用
新建Java類,繼承AppComatActivity類,并重寫onCreate方法
public class TextViewActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
}
}在java代碼中調(diào)用文本視圖對象的setText方法設(shè)置文本
與前面不同的是不需要在xml文件中寫text標(biāo)簽,需要在java類中創(chuàng)建對象并調(diào)用setText方法。
public class TextViewActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
TextView tv_hello = findViewById(R.id.tv_hello);
tv_hello.setText(R.string.hello);// 調(diào)用setText方法設(shè)置文本
}
} 二、設(shè)置文本的大小
px:是手機(jī)屏幕的最小顯示單位,與設(shè)備的顯示屏有關(guān)。
dp/dip:是與設(shè)備無關(guān)的顯示單位,只與屏幕的尺寸有關(guān),相同尺寸手機(jī),即使分辨率不同,同dp組件占用屏幕比例也相同,如果屏幕尺寸差異過大,需要做dp適配。
sp:專門用來設(shè)置字體大小,在系統(tǒng)設(shè)置中可以調(diào)整字體大小,跟隨系統(tǒng)字體設(shè)置而變化。
在java代碼中調(diào)用setTextSize方法
新建XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>新建java類
public class TextSizeActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_size);
TextView tv_px = findViewById(R.id.tv_hello);
tv_px.setTextSize(30);//這里不用指定單位,默認(rèn)為sp,所以官方推薦字體設(shè)置單位是sp
}在XML文件中通過屬性android:textSize指定 XML文件
android:textSize="30px"/>
java類
public class TextSizeActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_size);
}
}三、設(shè)置文本顏色
在java代碼中調(diào)用setTextColor方法設(shè)置文本顏色
具體色值可從Color獲取

XML文件
<TextView
android:id="@+id/tv_code_system"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="設(shè)置系統(tǒng)自帶顏色"
android:textSize="17sp"/>java類
public class TextColorActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_color);
//布局文件中獲取文本視圖
TextView tv_code_system = findViewById(R.id.tv_code_system);
//設(shè)置文本顏色
tv_code_system.setTextColor(Color.GREEN);
}四、設(shè)置視圖的寬高
視圖寬度通過屬性android:layout_width表達(dá),高度通過android:layout_height
寬高的取值主要有下列三種:
- match_parent:表示與上級視圖保持一致
- wrap_content:表示與內(nèi)容自適應(yīng)
- 以dp為單位的具體尺寸
例:
//上面的
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//下面的
android:layout_width="match_parent"
android:layout_height="wrap_content"
五、設(shè)置視圖的間距
有兩種方式:采用layout_margin屬性、padding屬性
1、layout_margin
指定了當(dāng)前視圖與周圍平級視圖之間的距離,即外邊距,包括:layout_margin、layout_marginLeft、layout_marginTop、layout_marginRight、layout_marginBottom
2、padding
指定當(dāng)前視圖與內(nèi)部下級視圖之間的距離,即內(nèi)邊距,包括:padding、paddingLeft、paddingTop、paddingRight、paddingBottom
例:
View標(biāo)簽在LinearLayout內(nèi),所以為其下級視圖
<!-- 中間層的布局背景為黃色-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"http://外邊距20dp
android:background="#FFFF99"
android:padding="60dp">//內(nèi)邊距60dp
<!-- 最內(nèi)層的視圖背景為紅色-->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"/>
</LinearLayout>
六、設(shè)置視圖的對齊方式
有兩種方式:采用layout_gravity屬性、采用gravity屬性;
取值包括:left、top、right、bottom,可以用豎線連接各取值,如:left|top表示朝左上角對齊。
1、layout_gravity
指定當(dāng)前視圖相對于上級視圖的對齊方式。
2、gravity
指定下級視圖相對于當(dāng)前視圖的對齊方式。
例:
<!--第一個(gè)子布局背景為紅色,它在上級視圖中朝下對齊,下級視圖靠左對齊-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:padding="10dp"
android:background="#ff0000"
android:layout_gravity="bottom"
android:gravity="left">
<!-- 內(nèi)部視圖-->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff"/>
</LinearLayout>
<!--第二個(gè)子布局背景為紅色,它在上級視圖中朝上對齊,下級視圖靠右對齊-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:padding="10dp"
android:background="#ff0000"
android:layout_gravity="top">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff"
android:gravity="right"/>
</LinearLayout>
到此這篇關(guān)于Android文本與視圖基本操作梳理介紹的文章就介紹到這了,更多相關(guān)Android文本與視圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談 Android 7.0 多窗口分屏模式的實(shí)現(xiàn)
這篇文章主要介紹了淺談 Android 7.0 多窗口分屏模式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
AndroidStudio代碼達(dá)到指定字符長度時(shí)自動換行實(shí)例
這篇文章主要介紹了AndroidStudio代碼達(dá)到指定字符長度時(shí)自動換行實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例
本篇文章主要介紹了android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
android中GridView實(shí)現(xiàn)點(diǎn)擊查看更多功能示例
本篇文章主要介紹了android中GridView實(shí)現(xiàn)點(diǎn)擊查看更多功能示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Convert WebP to PNG using java
本文主要介紹Convert WebP to PNG using java,這里對 WebP 做了詳細(xì)說明,并講解Linux 環(huán)境下WebP 轉(zhuǎn)png格式的示例,有興趣的小伙伴可以參考下2016-08-08
Android實(shí)現(xiàn)沉浸式導(dǎo)航欄實(shí)例代碼
通過本文給大家分享android實(shí)現(xiàn)沉浸式導(dǎo)航欄實(shí)例代碼,代碼非常實(shí)用,需要的朋友可以參考下2016-05-05

