Andriod Layouts
来源:互联网 发布:淘宝小商品 编辑:程序博客网 时间:2024/06/15 20:19
刚写了端代码,把layout修改下就遇到问题了。
布局定义了用户界面的虚拟结构(比如对于activity和widget的UI)。可以有两种声明方式
1.在XML里声明。
2.在运行时声明布局元素实例。也就是也代码创建View 和 ViewGroup对象。
当编译程序,每个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资源类里引用,而不是本地类。
创建的一般模式是:
- 定义一个唯一的ID
<Buttonandroid:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/my_button_text"/>
- 在函数里可以通过此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()得到。
一般布局
线性布局
![](http://developer.android.com/images/ui/linearlayout-small.png)
它把它的子元素放进一个单一的垂直或者横向的组里。如果超过试图长度,它会创建scrollbar。
相对试图
![](http://developer.android.com/images/ui/relativelayout-small.png)
能够制定子元素的彼此间的相对位置。
Web View
![](http://developer.android.com/images/ui/webview-small.png)
显示 web 页面.
用适配器创建视图
如果没有事先在XML里定义视图,我们可以在AdapterView子类里激活试图布局。AdapterView作为数据源和AdapterView布局之间的适配器来检索data,并转换后加到AdapterView布局。
有:
List View
![](http://developer.android.com/images/ui/listview-small.png)
Displays a scrolling single column list.
Grid View
![](http://developer.android.com/images/ui/gridview-small.png)
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 callingtoString()
on each item and placing the contents in aTextView
.For example, if you have an array of strings you want to display in a
ListView
, initialize a newArrayAdapter
using 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 yourListView
: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 aTextView
(for example, if you want anImageView
for each array item), extend theArrayAdapter
class and overridegetView()
to return the type of view you want for each item. - Your app
SimpleCursorAdapter
- Use this adapter when your data comes from a
Cursor
. When usingSimpleCursorAdapter
, you must specify a layout to use for each row in theCursor
and which columns in theCursor
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 aCursor
containing a row for each person and columns for the names and numbers. You then create a string array specifying which columns from theCursor
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, theCursor
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 theCursor
using the provided layout by inserting eachfromColumns
item into the correspondingtoViews
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);
- Andriod Layouts
- Layouts
- Layouts
- Layouts
- Layouts
- andriod
- andriod
- Andriod
- andriod
- andriod
- Andriod
- Andriod
- Andriod
- Layouts介绍
- XML Layouts
- log4j layouts
- HTML Layouts
- android layouts
- 工具——UltraEdit 删除空行
- 百度2014秋季校园招聘-软件研发工程师笔试题(2013年10月13日北京清华站)
- 将 Win32 程序移植到 Linux
- NAT穿透的详解及分析(NAT类型举例解释及穿透技术)
- 8个排序算法
- Andriod Layouts
- ios生命周期 uiviewController的生命周期(3)
- 数据库锁机制
- HTTP协议(一)HTTP报文
- 在Myeclipse里使用Junit贴图教程与使用JUnit在struts+spring+hibernate框架环境下进行单元测试
- hadoop-----命令小结
- JS字符串操作(添加删除,以逗号分隔)
- 什么是GitHub?
- steerpyremid分解重构