赞
踩
下面介绍一下android如何设置文本内容、设置文本字体大小、文本颜色、视图宽高、视图间距、和视图的对齐方式,只简单说明一下如何操作,具体知识后续。
mainifests子目录:下面的AndroidMainifest.xml文件是APP的运行配置文件。
java子目录:下面有三个com.example.myapp包,第一个包存放当前模块的java源代码,后面两个包存放测试用的java代码。
res子目录:存放当前模块的资源文件,有四个子目录:
有两种方式:在XML文件中设置和在java类中调用settext方法。
新建后会有以下默认内容:
- <?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>
在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的文本
- <resources>
- <string name="app_name">test</string>
- <string name="hello">你好</string>
- </resources>
新建Java类,继承AppComatActivity类,并重写onCreate方法
- public class TextViewActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_text_view);
- }
- }
与前面不同的是不需要在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:专门用来设置字体大小,在系统设置中可以调整字体大小,跟随系统字体设置而变化。
- <?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>
- 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
- }
-
- android:textSize="30px"/>
- public class TextSizeActivity extends AppCompatActivity {
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_text_size);
- }
- }
具体色值可从Color获取
- <TextView
- android:id="@+id/tv_code_system"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="设置系统自带颜色"
- android:textSize="17sp"/>
- 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
宽高的取值主要有下列三种:
例:
- //上面的
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- //下面的
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
有两种方式:采用layout_margin属性、padding属性
指定了当前视图与周围平级视图之间的距离,即外边距,包括:layout_margin、layout_marginLeft、layout_marginTop、layout_marginRight、layout_marginBottom
指定当前视图与内部下级视图之间的距离,即内边距,包括:padding、paddingLeft、paddingTop、paddingRight、paddingBottom
例:
View标签在LinearLayout内,所以为其下级视图
- <!-- 中间层的布局背景为黄色-->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="20dp"//外边距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表示朝左上角对齐。
指定当前视图相对于上级视图的对齐方式。
指定下级视图相对于当前视图的对齐方式。
例:
- <!--第一个子布局背景为红色,它在上级视图中朝下对齐,下级视图靠左对齐-->
- <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>

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。