網站首頁 編程語言 正文
目錄文件說明
mainifests子目錄:下面的AndroidMainifest.xml文件是APP的運行配置文件。
java子目錄:下面有三個com.example.myapp包,第一個包存放當前模塊的java源代碼,后面兩個包存放測試用的java代碼。
res子目錄:存放當前模塊的資源文件,有四個子目錄:
- drawable目錄存放圖形描述文件與圖片文件
- layout目錄存放app頁面的布局文件
- mipmap存放app的啟動圖標
- values存放一些常量定義文件,如字符串常量string.xml等。
一、設置文本內容
有兩種方式:在XML文件中設置和在java類中調用settext方法。
在XML文件中通過屬性android:text設置
1、在layout文件下新建一個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"> </LinearLayout>
2、配置XML文件設置文本
在LinearLayout標簽里添加以下內容;
text標簽里不寫具體的內容是因為:
情況:多個文件都用了相同的內容,但是需要修改此內容,那么就需要修改多個文件,而把內容寫在string文件里,則只需要修改string文件的內容。
<TextView android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello"/> //調用string文件里name為hello的文本
3、string文件內容
<resources> <string name="app_name">test</string> <string name="hello">你好</string> </resources>
4、java類調用
新建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代碼中調用文本視圖對象的setText方法設置文本
與前面不同的是不需要在xml文件中寫text標簽,需要在java類中創建對象并調用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);// 調用setText方法設置文本
}
}
二、設置文本的大小
px:是手機屏幕的最小顯示單位,與設備的顯示屏有關。
dp/dip:是與設備無關的顯示單位,只與屏幕的尺寸有關,相同尺寸手機,即使分辨率不同,同dp組件占用屏幕比例也相同,如果屏幕尺寸差異過大,需要做dp適配。
sp:專門用來設置字體大小,在系統設置中可以調整字體大小,跟隨系統字體設置而變化。
在java代碼中調用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);//這里不用指定單位,默認為sp,所以官方推薦字體設置單位是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);
}
}
三、設置文本顏色
在java代碼中調用setTextColor方法設置文本顏色
具體色值可從Color獲取
XML文件
<TextView android:id="@+id/tv_code_system" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設置系統自帶顏色" 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);
//設置文本顏色
tv_code_system.setTextColor(Color.GREEN);
}
四、設置視圖的寬高
視圖寬度通過屬性android:layout_width表達,高度通過android:layout_height
寬高的取值主要有下列三種:
- match_parent:表示與上級視圖保持一致
- wrap_content:表示與內容自適應
- 以dp為單位的具體尺寸
例:
//上面的 android:layout_width="wrap_content" android:layout_height="wrap_content" //下面的 android:layout_width="match_parent" android:layout_height="wrap_content"
五、設置視圖的間距
有兩種方式:采用layout_margin屬性、padding屬性
1、layout_margin
指定了當前視圖與周圍平級視圖之間的距離,即外邊距,包括:layout_margin、layout_marginLeft、layout_marginTop、layout_marginRight、layout_marginBottom
2、padding
指定當前視圖與內部下級視圖之間的距離,即內邊距,包括:padding、paddingLeft、paddingTop、paddingRight、paddingBottom
例:
View標簽在LinearLayout內,所以為其下級視圖
<!-- 中間層的布局背景為黃色--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp"http://外邊距20dp android:background="#FFFF99" android:padding="60dp">//內邊距60dp <!-- 最內層的視圖背景為紅色--> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000"/> </LinearLayout>
六、設置視圖的對齊方式
有兩種方式:采用layout_gravity屬性、采用gravity屬性;
取值包括:left、top、right、bottom,可以用豎線連接各取值,如:left|top表示朝左上角對齊。
1、layout_gravity
指定當前視圖相對于上級視圖的對齊方式。
2、gravity
指定下級視圖相對于當前視圖的對齊方式。
例:
<!--第一個子布局背景為紅色,它在上級視圖中朝下對齊,下級視圖靠左對齊--> <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"> <!-- 內部視圖--> <View android:layout_width="100dp" android:layout_height="100dp" android:background="#00ffff"/> </LinearLayout> <!--第二個子布局背景為紅色,它在上級視圖中朝上對齊,下級視圖靠右對齊--> <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>
原文鏈接:https://blog.csdn.net/Tir_zhang/article/details/126772429
相關推薦
- 2022-12-24 Kotlin協程Flow生命周期及異常處理淺析_Android
- 2022-05-06 python 實現兩個excel表格數據的對比
- 2022-05-25 ASP.NET?MVC+EF實現異步增刪改查_實用技巧
- 2023-01-08 ubuntu1804搭建FTP服務器的詳細教程_FTP服務器
- 2022-12-08 C語言如何實現成績等級判別_C 語言
- 2022-05-08 ASP.NET?MVC對URL匹配操作_實用技巧
- 2022-06-06 Python數據可視化Pyecharts制作Heatmap熱力圖_python
- 2022-05-01 Python中的bytes類型用法及實例分享_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支