android的布局资源,Android 布局资源

在Android中,屏幕的视图通常从XML文件加载。这些XML文件称为布局资源。

例子

下面显示了布局文件的示例<?xml version="1.0" encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/text1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

android:id="@+id/b1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

注意

/res/layout/子目录中的每个文件都将基于扩展名被排除的文件的名称生成一个唯一的常量。

有了布局,重要的是文件的数量。使用字符串资源,重要的是文件中单个字符串资源的数量。

例如,如果在/res/layout/下有两个文件,名为file1.xml和file2.xml,那么在R.java中有以下条目。public static final class layout {

//.... any other files public static final int file1=0x7f030000;

public static final int file2=0x7f030001;

//....}

参考

在这些布局文件中定义的视图,如:TextView,可以通过其在R.java中生成的资源ID在Java代码中访问:TextView tv = (TextView)this.findViewById(R.id.text1);

tv.setText("Try this text instead");

常量R.id.text1对应于为TextView定义的ID。布局文件中TextView的ID如下所示:

..

id属性的值表示使用称为text1的常量来唯一标识此视图。

@+id/text1中的加号(+)意味着将创建ID text1(如果它不存在)。

在布局资源中使用字符串资源

将字符串定义为资源后,你可以直接在视图上设置它们。

下面显示了一个示例,其中HTML字符串设置为TextView的文本内容。

以下代码用于string.xml。

simple string

Hello Slanted Android, You are bold.

以下代码用于layout.xml。

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:text="@string/tagged_string"/>

TextView自动实现这个字符串是一个HTML字符串,并相应的遵守其格式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值