现在的位置: 首页 > 综合 > 正文

Andriod Layouts

2013年05月27日 ⁄ 综合 ⁄ 共 4775字 ⁄ 字号 评论关闭

刚写了端代码,把layout修改下就遇到问题了。

布局定义了用户界面的虚拟结构(比如对于activity和widget的UI)。可以有两种声明方式

1.在XML里声明。

2.在运行时声明布局元素实例。也就是也代码创建View 和 ViewGroup对象。

装载XML

当编译程序,每个XML布局文件被编译到View资源。在Activity.onCreate() callback实现里必须装载布局资源。比如我们的XML资源保存在main_layout.xml

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

当Activirty启动时,onCreate() callback 方法被Andriod框架调用。

属性

ID

ID在编译时会被编译为整数,但在XML里在ID属性里写为string。

android:id="@+id/my_button"

@作用是在XML解析器里把后面的字符串作为ID属性来解析。+意思是新资源名字必须被创建和加进我们的R.java文件里。Andriod框剪会提供另外一个ID给这个资源。当音乐一个资源ID时,没有必要写出+,但是必须加一个andriod包命名空间,如下:

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

注意这是从android.R资源类里引用,而不是本地类。

创建的一般模式是:

  1. 定义一个唯一的ID
    <Buttonandroid:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/my_button_text"/>
  2. 在函数里可以通过此ID来引用这个资源,比如
    Button myButton =(Button) findViewById(R.id.my_button);

ID可以不是整个tree唯一的,但它应该是你正在查找的tree唯一。最好是全局唯一。

Layout参数

在XML里layout_something定义了布局参数。

每个ViewGroup类扩展了ViewGroup.LayoutParams。他的子类包含了定义每个子类试图的大小和位置的属性。在图1里,父类view
group为每个子类试图定义了布局参数。

所以的view group包含layout_width and layout_height,每个视图类必须定义他们。margins
and borders是可选的。

可以如此设置高和宽:

  • wrap_content告诉view要求它的内容范围的大小。
  • fill_parent告诉你的view变成parent view组运行的最大面积。

一般,声明高和宽用绝对单位是不推荐的。用相对较好,因为它帮助确定app在一系列不同屏幕大小的设备上显示(跨屏幕,哈哈)。

布局位置

布局是二维的,分为width和height。位置的维度的单位是像素。

APIgetLeft() and getTop()返回相对于他们父视图的位置。比如getLeft()
returns 20,意思是离它的父视图左边缘有20个像素。

大小,填充和边缘


一组视图有两组宽高值。

第一组是measured width and measured
height
.他们是在父视图内的。可以通过getMeasuredWidth() andgetMeasuredHeight()得到。

第二组是drawing width and drawing
height
.也就是在屏幕上实际显示的大小。可以和measured width and measured
height不同,通过getWidth() and getHeight()得到。

一般布局

线性布局

它把它的子元素放进一个单一的垂直或者横向的组里。如果超过试图长度,它会创建scrollbar。

相对试图

能够制定子元素的彼此间的相对位置。

Web View

显示 web 页面.

用适配器创建视图

如果没有事先在XML里定义视图,我们可以在AdapterView子类里激活试图布局。AdapterView作为数据源和AdapterView布局之间的适配器来检索data,并转换后加到AdapterView布局。

有:

List View

Displays a scrolling single column list.

Grid View

Displays a scrolling grid of columns and rows.

用数据填充适配器视图

可以通过变顶一个AdapterView实例到一个Adapter,它会从外部源来检索数据。

最常用的两种适配器是:

ArrayAdapter
Use this adapter when your data source is an array. By default, ArrayAdapter creates a view for each array item by calling toString() on
each item and placing the contents in a TextView.

For example, if you have an array of strings you want to display in a ListView, initialize a new ArrayAdapterusing
a constructor to specify the layout for each string and the string array:

ArrayAdapter adapter =newArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, myStringArray);

The arguments for this constructor are:

  • Your app Context
  • The layout that contains a TextView for each string in the array
  • The string array

Then simply call setAdapter() on your ListView:

ListView listView =(ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

To customize the appearance of each item you can override the toString() method for the objects in your array. Or,
to create a view for each item that's something other than a TextView (for example, if you want anImageView for
each array item), extend the ArrayAdapter class and override getView() to
return the type of view you want for each item.

SimpleCursorAdapter
Use this adapter when your data comes from a Cursor. When using SimpleCursorAdapter,
you must specify a layout to use for each row in the Cursor and which columns in the Cursor should
be inserted into which views of the layout. For example, if you want to create a list of people's names and phone numbers, you can perform a query that returns a Cursor containing
a row for each person and columns for the names and numbers. You then create a string array specifying which columns from the Cursor you want in
the layout for each result and an integer array specifying the corresponding views that each column should be placed:

 

String[] fromColumns ={ContactsContract.Data.DISPLAY_NAME, 
                        ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews ={R.id.display_name, R.id.phone_number};

When you instantiate the SimpleCursorAdapter, pass the layout to use for each result, the Cursor containing
the results, and these two arrays:

SimpleCursorAdapter adapter =newSimpleCursorAdapter(this, 
        R.layout.person_name_and_number, cursor, fromColumns, toViews,0);
ListView listView = getListView();
listView.setAdapter(adapter);

The SimpleCursorAdapter then creates a view for each row in the Cursor using
the provided layout by inserting each fromColumns item into the corresponding toViews view.

.

If, during the course of your application's life, you change the underlying data that is read by your adapter, you should call notifyDataSetChanged().
This will notify the attached view that the data has been changed and it should refresh itself.

Handling click events

You can respond to click events on each item in an AdapterView by implementing theAdapterView.OnItemClickListener interface.
For example:

// Create a message handling object as an anonymous class.
privateOnItemClickListener mMessageClickedHandler =newOnItemClickListener(){
    publicvoid onItemClick(AdapterView parent,View v,int position,long id){
        // Do something in response to the click
    }
};

listView.setOnItemClickListener(mMessageClickedHandler);

抱歉!评论已关闭.