安卓-视图设置

kk3TWT Lv3

视图指 View 类,所有的控件类(如 TextView、Button等)都继承自 View 类,可以说程序内可见的所有控件都是视图

设置视图宽高

在 XML 文件中,通过属性 android:layout_width 设置视图宽度,通过属性 android:layout_height 设置视图高度,主要的取值有:

  1. match_parent:匹配父组件,就是父组件多大,子组件就多大
  2. wrap_content:包裹内容,与内容自适应
  3. 具体尺寸,以 dp 为单位
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world (wrap_content)"
android:textSize="15sp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello world (match_parent)"
android:textSize="15sp" />

<TextView
android:layout_width="350dp"
android:layout_height="100dp"
android:text="hello world (firm size)"
android:textSize="15sp"/>

在 Java 代码中,要设置视图的宽高,需要先确保它在 XML 中的宽高属性被设置为 wrap_content ,然后获取控件对象,再依次执行以下步骤(参数单位为 px ,注意单位转换):

  1. 调用 getLayoutParams 方法获取布局参数
  2. 修改 width 和 height 参数,分别表示宽度和高度
  3. 调用 setLayoutParams 方法修改布局参数

也可以使用 setWidth 和 setHeight 方法来设置宽高属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tv = findViewById(R.id.set_by_code);

// 使用getLayoutParam和setLayoutParam方法
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.width = 500;
params.height = 300;
tv.setLayoutParams(params);

// 使用setWidth和getWidth方法
tv.setWidth(500);
tv.setHeight(300);
}

设置视图间距

在 XML 文件中,有两种属性定义视图间距:

  1. layout_margin 属性:外边距,指定当前视图与周围平级视图之间的距离(包括margin、marginTop、marginBottom、marginLeft、marginRight,即本体和上下左右
  2. padding 属性:内边距,指定当前视图与内部下级视图之间的距离(也包括本体和上下左右

**注意:**设置本体的值,就是一起设置上下左右的值(如margin = 10dp,则上下左右的值都是 10dp )

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
<?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:background="@color/teal_200"
android:orientation="vertical"
android:gravity="center"

android:padding="10dp"> <!-- 设置内边距 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#3388FF"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/purple_200"

android:layout_margin="20dp"
android:padding="10dp"> <!-- 设置外边距和内边距 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AABBCC"/>

</LinearLayout>

</LinearLayout>

设置视图对齐方式

在 XML 文件中,有两个属性设置视图对齐方式:

  1. layout_gravity 属性:当前视图相对于上级视图的对齐方式
  2. gravity 属性:下级视图相对于当前视图的对齐方式

这两个属性的取值有:left、top、right、bottom等,这些取值可以使用 ”|“ 连接,比如 ” left|top “ 指靠左上对齐

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
<?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:background="@color/teal_200"
android:orientation="horizontal">

<!-- 第一个子布局,朝下对齐,其下级布局靠左对齐 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#3388FF"
android:padding="10dp"

android:gravity="left"
android:layout_gravity="bottom"> <!-- 本体靠下,子布局靠左 -->

<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFEEAA"/>

</LinearLayout>

<!-- 第二个子布局,朝上对齐,其下级布局靠右对齐 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#3388FF"
android:padding="10dp"

android:gravity="right"
android:layout_gravity="top"> <!-- 本体靠上,子组件靠右 -->

<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#AABBCC"/>

</LinearLayout>

</LinearLayout>
  • 标题: 安卓-视图设置
  • 作者: kk3TWT
  • 创建于 : 2026-04-11 22:17:10
  • 更新于 : 2026-04-11 22:21:03
  • 链接: https://kk-is-very-happy.online/posts/70152484/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。