Android XML布局
前言
今天我们来简单介绍一下Android的XML布局以及三种常见的XML布局
目录
1.XML布局的优点
在XML文件中设计UI可以更好地将应用的外观与控制应用行为的代码隔离,每次修改或调整界面布局只需要修改XML文件的代码而不是修改源码和重新编译。
2.XML布局基础介绍
2.1 调用XML布局
在编译XML布局文件时,都需要通过main函数中的 onCreate 来调用,通常使用如下方式来进行调用。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_layout);
}
其中setContentView里的内容‘R’为就是系统给声明的静态变量,所有的子类都是通过‘R’来调用。而‘R’的路径则是 Android 工程路径下的 app -> src -> main -> res 下。
R.layout指res文件夹下一个名为layout的文件夹,而R.layout.activity_linear_layout之后的部分则是layout文件夹下自己定义的.xml文件。
2.2 XML布局的常见属性
属性 | 描述 |
---|---|
ID | 控件名字,是当前控件的唯一标识,常用字符串表示 |
background | 调整当前界面背景 |
gravity | 调整所有控件所在的整体位置 |
layout_width | 当前界面整体宽度,常用wrap_content和match_parent |
layout_height | 当前界面整体高度,常用wrap_content和match_parent |
padding | 调整屏幕边距,可以整体调整或者单项调整 |
text | 控件显示文本,可调用values文件夹中的strings.xml来显示文本内容 |
textColor | 显示文本颜色,可调用values文件夹中的color.xml来定义 |
textSize | 显示文本字体大小,单位常用dp和px |
textStyle | 显示文本字体风格,可选的有斜体和粗体 |
3.常见的三种简单XML布局
3.1 LinearLayout
这一种使用单个水平行或垂直行来组织子项的布局,是风格最简单的布局。
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:weightSum="1"
android:id="@+id/linearLayout"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentRight="false"
android:layout_alignParentBottom="false"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="One,One"
android:id="@+id/textView2"
android:textSize="16dp"
android:gravity="center_horizontal"
android:layout_weight="0.19"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="One,Two"
android:id="@+id/textView3"
android:textSize="16dp"
android:layout_weight="0.34"
android:gravity="center_horizontal"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="One,Three"
android:id="@+id/textView4"
android:layout_weight="0.27"
android:textSize="16dp"
android:gravity="center_horizontal"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="One,Four"
android:id="@+id/textView5"
android:textSize="16dp"
android:gravity="center_horizontal"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/linearLayout"
android:weightSum="1"
android:id="@+id/linearLayout2"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Two,One"
android:id="@+id/textView6"
android:textSize="16dp"
android:layout_weight="0.13"
android:gravity="center_horizontal"
android:textColor="#ffffff" />
<TextView
android: