android <初级篇> 界面布局<一> ——菜鸟的学习之路

   对于应用程序来说,用户体验至为重要,而用户体验的第一站就是应用程序的界面。一个拥有友好界面布局的应用程序能在第一时间内给用户一个好的感觉,让用户有继续下面操作的想法。

 

在移动应用泛滥的信息社会,一款真正的体验良好的应用程序才能受大众的青睐。

 

   对于开发人员来说,设计也是必不可少的。当然,在android的应用开发中,我们可以在类中进行设计,也可以直接使用xml的布局文件进行设计。为了让视图与业务逻辑处理实现松耦合性,建议使用xml的布局文件对应用程序进行设计。

 

1. 布局文件所在的位置:

res/layout/filename.xml

注意:这里的filename.xml是用户自定义的布局文件,布局文件名中不能有大写。

 

2. 布局文件的引用:

在Java文件中使用语句: R.layout.filename

3. 布局文件语法:

<?xml version="1.0" encoding="utf-8"?>
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@[+][package:]id/resource_name"
    android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
    android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
    [ViewGroup-specific attributes] >
    <View
        android:id="@[+][package:]id/resource_name"
        android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
        android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
        [View-specific attributes] >
        <requestFocus/>
    </View>
    <ViewGroup >
        <View />
    </ViewGroup>
    <include layout="@layout/layout_resource"/>
</ViewGroup>


 

注意:布局文件的根元素可以是ViewGroup、View或者是 <merge>元素,但是,必须有且只有一个根元素,在根元素中,必须要包含xmlns:android属性的命名空间。

 

4. 接下来,分析一下布局文件中的元素。

<ViewGroup>

这个元素可以包含其他的View元素。

 

ViewGroup类是一个抽象类,如果使用类文件进行布局的话,显然不能使用new直接创建,所以只能使用其子类(LinearLayout、RelativeLayout、FrameLayout)进行实例化。

在布局文件中,也可以使用这些标签作为根元素标签。

 

属性:

--> android:id  引用资源的id.用于唯一确定一个元素或控件。你可以在其他的View、Activity或业务处理类中通过id属性引用该控件。

 

--> android:layout_height  控件的高度(必须的属性),可使用的值有:"fill_parent"、"wrap_content"及"match_parent"。

 

--> android:layout_width 控件的宽度(必须的属性),可使用的值与android:layout_height一样。

但必须注意的是,如果屏幕上一列要显示多个控件时,则不能将android:height的值设为match_parent或fill_parent

同样,如果屏幕上一行要显示多个控件时,则不能将android:width的值设为match_parent和fill_parent.

 

-->  <requestFocus /> 在屏幕上初始化父级容器并使其获得焦点。 在一个布局文件中,只能使用一次。

 

-->  <include> 将其他的布局文件中的控件加入到当前布局中。

     属性layout : 值为其他布局文件的引用(该属性是必须的)

 

5.使用示例

--> android:id  <TextView android:id="@+id/nameTextbox"/>

    Java中: findViewById(R.id.nameTextbox);  返回值是TextView对象

 

--> 一个简单的布局使用实例(来自android docs的实例)

 

res/layout/main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>


在Activity中的onCreate()方法应用布局文件

方法如下:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }


你还可以使用自定义的视图,详细方法参考:http://developer.android.com/guide/topics/ui/custom-components.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值