android之layout配置文件解读

本文解析了Android中ListView和TextView在布局文件中的使用方法,包括如何通过ID资源引用组件及利用layout_weight属性来分配视图占比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这样的解读在
[url]http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html[/url]

标记上,以备忘记


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_notes"/>

</LinearLayout>




The [color=blue]@ [/color]symbol in the id strings of the [color=blue]ListView[/color] and [color=blue]TextView[/color] tags means that the XML parser should parse and expand the rest of the id string and use an ID resource.
The [color=blue]ListView[/color] and [color=blue]TextView[/color] can be thought as two alternative views, only one of which will be displayed at once. [color=blue]ListView[/color] will be used when there are notes to be shown, while the [color=blue]TextView [/color](which has a default value of "No Notes Yet!" defined as a string resource in [color=green]res/values/strings.xml[/color]) will be displayed if there aren't any notes to display.
The list and empty IDs are provided for us by the Android platform, so, we must prefix the id with [color=blue]android: [/color](e.g., @android:id/list).
The [color=blue]View [/color]with the empty id is used automatically when the [color=blue]ListAdapter [/color]has no data for the [color=blue]ListView[/color]. The [color=blue]ListAdapter [/color]knows to look for this name by default. Alternatively, you could change the default empty view by using [color=blue]setEmptyView[/color](View) on the [color=blue]ListView[/color].

More broadly, the [color=darkblue]android.R[/color] class is a set of predefined resources provided for you by the platform, while your project's [color=darkblue]R[/color] class is the set of resources your project has defined. Resources found in the [color=darkblue]android.R[/color] resource class can be used in the XML files by using the [color=blue]android: [/color]name space prefix (as we see here).



<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>



This is the View that will be used for each notes title row — it has only one text field in it.

In this case we create a new id called [color=blue]text1[/color]. The [b]+ [/b]after the [b]@[/b] in the id string indicates that the id should be automatically created as a resource if it does not already exist, so we are defining [color=blue]text1 [/color]on the fly and then using it.




<?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">

<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<EditText android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/body" />
<EditText android:id="@+id/body" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />

<Button android:id="@+id/confirm"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>




[color=blue]layout_weight[/color] is used in LinearLayouts to assign "importance" to Views within the layout.[b] All Views have a default layout_weight of zero, meaning they take up only as much room on the screen as they need to be displayed.[/b] Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View's [color=blue]layout_weight[/color] and its ratio to the overall [color=blue]layout_weight [/color]specified in the current layout for this and other View elements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值