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

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


新建后會(huì)有以下默認(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)用文本視圖對(duì)象的setText方法設(shè)置文本
與前面不同的是不需要在xml文件中寫text標(biāo)簽,需要在java類中創(chuàng)建對(duì)象并調(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:表示與上級(jí)視圖保持一致
- 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)前視圖與周圍平級(jí)視圖之間的距離,即外邊距,包括:layout_margin、layout_marginLeft、layout_marginTop、layout_marginRight、layout_marginBottom
2、padding
指定當(dāng)前視圖與內(nèi)部下級(jí)視圖之間的距離,即內(nèi)邊距,包括:padding、paddingLeft、paddingTop、paddingRight、paddingBottom
例:
View標(biāo)簽在LinearLayout內(nèi),所以為其下級(jí)視圖
<!-- 中間層的布局背景為黃色-->
<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è)置視圖的對(duì)齊方式
有兩種方式:采用layout_gravity屬性、采用gravity屬性;
取值包括:left、top、right、bottom,可以用豎線連接各取值,如:left|top表示朝左上角對(duì)齊。
1、layout_gravity
指定當(dāng)前視圖相對(duì)于上級(jí)視圖的對(duì)齊方式。
2、gravity
指定下級(jí)視圖相對(duì)于當(dāng)前視圖的對(duì)齊方式。
例:
<!--第一個(gè)子布局背景為紅色,它在上級(jí)視圖中朝下對(duì)齊,下級(jí)視圖靠左對(duì)齊-->
<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è)子布局背景為紅色,它在上級(jí)視圖中朝上對(duì)齊,下級(jí)視圖靠右對(duì)齊-->
<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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android使用OKHTTP解析JSON數(shù)據(jù)的實(shí)例代碼
本篇文章主要介紹了Android使用OKHTTP解析JSON數(shù)據(jù)的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android實(shí)現(xiàn)頂部底部雙導(dǎo)航界面功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)頂部\底部雙導(dǎo)航界面功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android Studio實(shí)現(xiàn)仿微信APP門戶界面詳解及源碼
這篇文章帶你通過Android studio來實(shí)現(xiàn)微信APP的門戶界面,主要說明框架的各部分功能與實(shí)現(xiàn)過程,下文包含了整個(gè)開發(fā)過程,以及解決問題的思路并再末尾提供了源碼鏈接2021-10-10
Android實(shí)現(xiàn)短信發(fā)送功能
這篇文章主要介紹了Android實(shí)現(xiàn)短信發(fā)送功能,對(duì)Android實(shí)現(xiàn)短信發(fā)送的每一步都進(jìn)行了詳細(xì)的介紹,感興趣的小伙伴們可以參考一下2015-12-12
Android Studio 視頻播放失敗 start called in state1 異常怎么解決
很多朋友問小編在使用MediaPlayer播放音頻時(shí)報(bào)出 E/MediaPlayerNative: start called in state 1, mPlayer(0x0)問題,該如何處理呢,今天小編給大家?guī)砹薃ndroid Studio 視頻播放失敗 start called in state1 異常問題,需要的朋友可以參考下2020-03-03
百度語音識(shí)別(Baidu Voice) Android studio版本詳解
這篇文章主要介紹了百度語音識(shí)別(Baidu Voice) Android studio版本詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android DrawerLayout實(shí)現(xiàn)側(cè)拉菜單功能
這篇文章主要介紹了Android DrawerLayout實(shí)現(xiàn)側(cè)拉菜單功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06

