Android UI 开发
Android 中的 UI
Android 应用的界面是由 View 和 ViewGroup 对象构建的。它们有很多种类,并且都是 View 类的子类。
ViewGroup 是 View 的一个扩展,可以容纳多个 View,通过 ViewGroup 类可以创建有联系的子 View 组成的复合控件。
线性布局 (LinearLayout)
线性布局是 Android 中较为常用的布局方式,它使用标签。线性布局主要有两种形式,一种是水平线性布局,一种是垂直线性布局,如图所示:
UI 界面的开发
1. 在 Androidstudio 开发环境中建立 UI 界面切换至 Android 视图依次打开 app-res-layout,在 layout 中选择新建 XML 文件 (linearlayoutdemo1.xml) 编写如下代码,通过修改代码进行线性布局的水平或者垂直两种形式的转换,其代码如下:
- android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button2"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button3"/>
2. 区分属性
android:layout_gravity 用于指定控件在布局中的对齐方式。
android:gravity 用于指定文字在控件中的对齐方式。
建立名为 linearlayoutdemo2.xml 的 XML 文件进行 UI 界面中控件的位置变换,代码如下:
- android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="top"android:text="Button1"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="Button2"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="Button3"/>
UI 界面效果如图
3. 重要属性—android:layout_weight
android:layout_weight 属性允许我们使用比例的方式来指定空间的大小,它在手机屏幕的适配性方面可以起到非常重要的作用。
建立名为 linearlayoutdemo3.xml 的 XML 文件运用 android:layout_weight 属性编写如下代码:
- android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:hint="Type someting" />android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Send" />
UI 界面呈现如下效果,体现了 android:layout_weight 属性的适配性。
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/03-03/18000195.html