您的位置:首页 > 移动开发 > Android开发

Android 开发指南 翻译2 User Interface:XML Layouts

2012-02-04 15:01 531 查看
Android 开发指南翻译2 User Interface:XML Layouts

定义:Your layout is the architecture for the user interface in an Activity. It defines
the layout structure and holds all the elements that appear to the user. 

定义:layout是在Activity中的用户界面结构。包括:结构和元素。

声明的方法:

>在xml文件中定义UI元素

>在程序运行时,实例化layout元素

两种方法可以交替使用。

The advantage to declaring your UI in XML is that it enables you to better separate the presentation
of your application from the code that controls its behavior. 


Write the 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>

保存路径 res/layout下。


Load the XML Resource

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

文件main_layout.xml布局文件载入。


Attributes

Some attributes are specific to a View object,And, other attributes
are considered "layout parameters," which are attributes that describe certain layout orientations of the View object, as defined by that object's parent ViewGroup object.

两种属性:

>View类属性:该View的属性。

>布局属性:描述视图的布局方向。

ID:Any View object
may have an integer ID associated with it, to uniquely identify the View within the tree.

android:id="@+id/my_button"

@引导符

+ 增加一个id

使用Android框架的id

android:id="@android:id/empty"


Layout Parameters 

XML layout attributes named 
layout_something
 define layout parameters
for the View that are appropriate for the ViewGroup in which it resides.

LayoutParams are used by views to tell their parents how they
want to be laid out. 

Layout 参数:属性layout_XXXX是定义View在ViewGroup
的适当位置。View的使用这些属性用来告诉他们的父对象,他们想如何摆放。
layout_width,layout_height他们的值可以是:固定值dip,dp,px;wrap_content;fill_parent

Layout Position

view是以矩形区域,通过getLeft(),getTop(),getRight(),getBottom()获取相对于父对象的位置,单位是px。


Size, Padding and Margins

The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

View拥有2对宽高值。

The
first pair
is known as measured width and measured height. These dimensions define how big a view wants to be within its parent. The measured dimensions can be obtained by calling 
getMeasuredWidth()
 and 
getMeasuredHeight()
.

>第一对儿:测量宽高:定义在父对象中的大小。

The
second pai
r is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout.The
width and height can be obtained by calling 
getWidth()
 and 
getHeight()
.

>第二对儿:绘制宽高:定制实际绘制的大小。

Padding
can be used to offset the content of the view by a specific amount of pixels.

Padding是View内容与View的边界。(区别于Margin:是view和其他view的边界。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息