在样式中定义ID,是安全还是灾难?

时间:2011-04-05 20:55:56

标签: android android-layout

以下问题让我困惑了一段时间,我想也许会问这不会有什么害处。我有以下layout.xml和style.xml文件;

RES /布局/ layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        style="@style/headerContainer" />
    <LinearLayout
        style="@style/footerContainer" />
    <ScrollView
        style="@style/contentContainer" />    
</RelativeLayout>

RES /值/ style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="container">
    <item name="android:layout_width">fill_parent</item>
    </style>
    <style name="headerContainer" parent="container">
        <item name="android:layout_height">40dp</item>
        <item name="android:layout_alignParentTop">true</item>
        <item name="android:background">#80FF0000</item>
        <item name="android:id">@+id/header</item>
    </style>
    <style name="footerContainer" parent="container">
        <item name="android:layout_height">50dp</item>
        <item name="android:layout_alignParentBottom">true</item>
        <item name="android:background">#8000FF00</item>
        <item name="android:id">@+id/footer</item>
    </style>
    <style name="contentContainer" parent="container">
        <item name="android:layout_height">60dp</item>
        <item name="android:layout_below">@id/header</item>
        <item name="android:layout_above">@id/footer</item>
        <item name="android:background">#800000FF</item>
    </style>
</resources>

现在,问题是,当我在style.xml中引入ID时,是否存在重叠ID的危险?有趣的是,这种方法在我至少使用的模拟器上有效,但创建的ID没有被添加到R类。一旦我的布局膨胀,我就会对它们的定义感到困惑。

3 个答案:

答案 0 :(得分:22)

不要在样式中使用@+id/... @+id/...只能用于布局 否则,您可以在构建期间获得Error executing apt: return code 139 如果需要,使用@id/...并使用帮助资源文件生成ID: RES /值/ ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="header" />
    <item type="id" name="footer" />
</resources>

http://developer.android.com/guide/topics/resources/more-resources.html#Id

答案 1 :(得分:1)

我的朋友不安全。您应该为不同的文件使用不同的ID。这里的模拟器不会出现问题。它会理解,因为每个xml文件都有自动定义R.java文件的唯一代码。所以模拟器很容易理解。但是如果你需要改进或编辑任何升级的代码,你肯定会混淆哪个id属于哪个xml文件的布局或者是楔形的。因此,为每个布局小部件提供唯一ID。如果您提供包含相应文件名的某个标记的ID,将会很有帮助。

示例:如果文件名是filldetails.xml,那么您可以使用id = @ + fd_name

了解申请流程会很有帮助。

答案 2 :(得分:0)

我这样做,祝你好运:

布局 RES /布局/ main.xml中

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <include layout="@layout/action_bar"/>
</RelativeLayout>

常见: RES /布局/ action_bar.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/action_bar_container"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/action_bar_height"
    android:layout_alignParentTop="true"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    >

    <TextView
        android:id="@+id/action_bar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="20dip"
        android:textStyle="bold"
        />

</RelativeLayout>