线性布局的使用
利用所学的UI开发方面的知识完成如下界面:
打开AndroidStudio开发环境,建立LinearLayout文件,其图一界面代码如下:
<?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:padding="20dp"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:layout_weight="1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00"
android:layout_weight="1"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:layout_weight="1"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f"
android:layout_weight="1"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f1"
android:layout_weight="1"
android:layout_marginTop="20dp"
/>
</LinearLayout>
其中
android:padding="20dp"含义是所建控件相对于布局上下左右均存在20dp的空间
android:layout_wight="1"运用权重使控件符合布局
android:layout_marginTop="20dp"表示离某元素上边缘的距离android:background="#f0f1"界面中的元素的颜色属性,通过修改"#"后边的数值改变颜色的配色方案
最后呈现出的界面图如下:
现在进行图二所示界面的编辑,同样的打开AndroidStudio开发环境,建立LinearLayout文件,其代码如下:
<?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"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subject"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message"
android:layout_weight="1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"
android:layout_gravity="bottom"
android:gravity="right"
/>
</LinearLayout>
同样的这其中也运用权重使控件符合布局,能适应不同尺寸的界面。
其界面最终呈现效果如下:
这样我们就按照要求完成了线性布局的简单应用,相信在以后的学习过程中 会在更多的方面用到线性布局方面的知识。