android main.xml 分析

来源:互联网 发布:手机照片编制软件 编辑:程序博客网 时间:2024/05/21 09:14

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/login"

/>

 

<EditText

        android:id="@+id/etUserName"

        android:layout_weight="1.0"

        android:maxLength="8"

        android:maxLines="1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text=""

    />

</LinearLayout>

 

 

1,<?xml version="1.0" encoding="utf-8"?>  xml标记描述语言,必须的

 

2<LinearLayout> 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的

 

3xmlns:android=http://schemas.android.com/apk/res/android

描述xml描述档案的名称空间。必须属性

 

 

4,android:orientation="vertical"

表示这个介质的版面配置方式是从上到下的垂直得排列其内部的视图

android:orientation="horizontal" 水平排列

 

5android:layout_width="fill_parent"

定义当前视图在屏幕上可以消费的宽度,fill_parent即填充整个屏幕。

android:layout_height="fill_parent"

 

"wrap_content":随着文字栏位的不同而改变这个视图的宽度或者高度。

有点自动设置框度或者高度的意思

6android:layout_weight="1.0"

Layout_weight属性是为了要EditText能延伸到最右侧【将剩余的空间全部给他】

layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个 文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之一分给第一个,三分之二分给第二个(因为我们声 明第二个有较之第一个更高的重要度)。

这个布局也演示了如何在其它布局中嵌套多个布局以实现更复杂更漂亮的布局。在本例中,一个水平方向上的线性布局嵌套在另一垂直方向上的布局中,以使标题标签和文本字段在水平方向上挨个对齐

 

<LinearLayout>

<Button android:text="@string/login"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1.0" />

<Button android:text="@string/cancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1.0" />

</LinearLayout>

 

这里的Layout_weight属性是为了要两个Button的宽度相等,同时又能填充满一行。因为1.0 1.0 == 11. 所以宽度相等了。

原创粉丝点击