Android UI 开发

来源:互联网 发布:八本兵种等级数据 编辑:程序博客网 时间:2024/05/18 21:07

Android UI 开发

Android中的UI

      Android应用的界面是由ViewViewGroup对象构建的。它们有很多种类,并且都是View类的子类。

      ViewGroupView的一个扩展,可以容纳多个View,通过ViewGroup类可以创建有联系的子View组成的复合控件。

线性布局(LinearLayout)

        线性布局是Android中较为常用的布局方式,它使用<LinearLayout>标签。线性布局主要有两种形式,一种是水平线性布局,一种是垂直线性布局,如图所示:


UI界面的开发

1.在Androidstudio开发环境中建立UI界面切换至Android视图依次打开app-res-layout,在layout中选择新建XML文件(linearlayoutdemo1.xml)编写如下代码,通过修改代码进行线性布局的水平或者垂直两种形式的转换,其代码如下:

  <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        
/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        
/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button3"
    
/>

2.区分属性

  android:layout_gravity用于指定控件在布局中的对齐方式。

  android:gravity用于指定文字在控件中的对齐方式。

  建立名为linearlayoutdemo2.xml的XML文件进行UI界面中控件的位置变换,代码如下:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:text="Button1"
    
/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Button2"
    
/>

<Button
    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属性编写如下代码:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="Type someting"
/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send"
/>

UI界面呈现如下效果,体现了 android:layout_weight属性的适配性。




 

2 0
原创粉丝点击