<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/welcome_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:layout_centerInParent="true"
android:textSize="28sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/version"
android:layout_centerInParent="true"
android:layout_below="@+id/welcome_tv"
android:textSize="16sp"
/>
</RelativeLayout>
代码解释:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 相对布局
xmlns:android="http://schemas.android.com/apk/res/android" xmlns开头的都是命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" 定义容器的宽度,容器宽度一般取值match_parent
android:layout_height="match_parent" 定义容器的高度,match_parent表示按照屏幕大小
tools:context=".MainActivity">
<TextView 文本控件
android:id="@+id/welcome_tv" 给控件取名字,名字id值是唯一的
android:layout_width="wrap_content" 定义控件的宽度,wrap_content表示按内容大小
android:layout_height="wrap_content" 定义控件的高度
android:text="欢迎使用智慧工厂应用" 定义控件的文本内容
android:layout_centerInParent="true" 定义控件的位置:父容器居中
android:textSize="28sp" /> 定义字体大小
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版本1.0"
android:layout_centerInParent="true"
android:layout_below="@+id/welcome_tv" 定义控件的位置,相对于某个控件的下面
android:textSize="16sp" 定义字体大小,单位为sp
/>
</RelativeLayout>
设置主界面运行:
上面的字符串也可以在values中的strings.xml中去定义,这个好处是方便后期维护修改
在布局中引用字符串资源: