android 视图基础

设置文本内容

ec7cb785770cc27d.png

代码实现:activity_text_view.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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"
android:gravity="center">

<TextView
android:id="@+id/nk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/he"/>


</LinearLayout>

TextViewActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.luowen.chapter03;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TextViewActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
TextView nk= findViewById(R.id.nk);
nk.setText("开发");

}
}

设置文本的大小

78cf9333e943a9f9.png

代码实现:activity_textsize.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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"
android:gravity="center" >

<TextView
android:id="@+id/kk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/he"
android:textSize="30px"
/>
<TextView
android:id="@+id/tk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/he"
android:textSize="30dp"
/>
<TextView
android:id="@+id/k"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/he"
android:textSize="30sp"
/>

</LinearLayout>

TextsizeActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.luowen.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class TextsizeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textsize);
TextView kk = findViewById(R.id.kk);
kk.setTextSize(120);
}
}

设置文本颜色

c86b0e22f64735a4.png

代码实现activity_text_color.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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"
android:gravity="center">

<TextView
android:id="@+id/tv_code_system"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代码设置系统自带的颜色"
android:textSize="17sp"/>
<TextView
android:id="@+id/tv_code_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代码设置8位文字颜色"
android:textSize="17sp"/>
<TextView
android:id="@+id/tv_code_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代码设置6位文字颜色"
android:textSize="17sp"/>
<TextView
android:id="@+id/tv_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布局文件设置6位文字颜色"
android:textSize="17sp"
android:textColor="#00ff00"/>
<TextView
android:id="@+id/tv_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="资源文件引用6位文字颜色"
android:textSize="17sp"
android:textColor="@color/green"/>
<TextView
android:id="@+id/tv_code_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="背景设置为绿色"
android:textSize="17sp" />
<!--android:background="@color/green" -->




</LinearLayout>

TextColorActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.luowen.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TextColorActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_color);
//从布局文件中获取 id 名为tv_code_system 文本视图
TextView tv_code_system = findViewById(R.id.tv_code_system);
//将tv_code_system 的文字颜色设置成系统自带的的绿色
tv_code_system.setTextColor(Color.GREEN);


//从布局文件中获取 名为tv_code_eight 的文本视图
TextView tv_code_eight = findViewById(R.id.tv_code_eight);
//将tv_code_eight 的文字颜色设置成不透明的绿色,即正常的绿色
tv_code_eight.setTextColor(0xFF00FF00);


//从布局文件中获取 名为tv_code_six 的文本视图
TextView tv_code_six = findViewById(R.id.tv_code_six);
//将tv_code_six 的文字颜色设置成透明的绿色,透明就是看不到
tv_code_six.setTextColor(0x00FF00);


//从布局文件中获取 tv_code_background 的文本视图
TextView tv_code_background = findViewById(R.id.tv_code_background);
//将 tv_code_background 的背景颜色设置成绿色
//tv_code_background.setBackgroundColor(Color.GREEN);
//颜色来自于资源文件
tv_code_background.setBackgroundResource(R.color.green);
}
}

设置视图宽高

acaafd3c080dde9d.png

效果图

ddf9451c3eeaf20f.png

代码实现activity_view_border.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="视图宽高采用wrap_content定义"
android:textColor="#000000"
android:background="#00ffff"
android:textSize="17sp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="视图宽度采用match_content定义"
android:textColor="#000000"
android:background="#00ffff"
android:textSize="17sp"/>

<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="视图宽度采用固定大小"
android:textColor="#000000"
android:background="#00ffff"
android:textSize="17sp"/>

<TextView
android:id="@+id/tv_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="通过代码指定视图宽度"
android:textColor="#000000"
android:background="#00ffff"
android:textSize="17sp"/>

</LinearLayout>

ViewBorderActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.luowen.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

import com.luowen.chapter03.util.Utils;

public class ViewBorderActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_border);
TextView tv_code = findViewById(R.id.tv_code);
//获取tv_code 的布局参数(包括宽度高度)
ViewGroup.LayoutParams params= tv_code.getLayoutParams();
//修改布局参数中的宽度数值,注意默认单位为px单位,需要把dp值改为px数值
params.width= Utils.dip2px(this,300);
tv_code.setLayoutParams(params);
}
}

util/utils.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.luowen.chapter03.util;

import android.content.Context;

public class Utils {
public static int dip2px (Context context,float dpValue){
//获取当前手机的像素密度(1个dp对应几个px)
float scale = context.getResources().getDisplayMetrics().density;
return (int)(scale*dpValue+0.5f);

}
}

设置视图的间距

cf0cf4cfbbd2a79c.png

视图代码activity_view_margin.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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="300dp"
android:orientation="vertical"
android:background="#00AAFF">

<!--中间层的布局背景为黄色 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="#FFFF99"
android:padding="60dp">

<!--最内层布局设置为红色 -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"></View>

</LinearLayout>

</LinearLayout>

设置视图的对齐方式

d7a4ccfa6cfdfef3.png

activity_view_gravity.xml 内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal"
android:background="#ffff99">

<!-- 第一个子布局背景为红色,它的上级视图中朝下对齐,它的下级视图靠左对齐 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="#ff0000"
android:padding="10dp"
android:layout_gravity="bottom"
android:gravity="left">

<!-- 内部视图的宽度和高度都是100dp,且背景色为青色-->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff">

</View>

</LinearLayout>

<!-- 第二个子布局背景为红色,它的上级视图中朝上对齐,它的下级视图靠右对齐 -->

<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="#ff0000"
android:padding="10dp"
android:layout_gravity="top"
android:gravity="right">

<!-- 内部视图的宽度和高度都是100dp,且背景色为青色-->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff">

</View>

</LinearLayout>


</LinearLayout>