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

har*_*ism 21 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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

Sen*_*sei 23

不要@+id/...在款式中使用.
@+id/...只能在布局中使用.
否则你可以Error executing apt: return code 139在构建期间获得.如果需要,
使用@id/...和生成带有帮助资源文件的id:res/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="header" />
    <item type="id" name="footer" />
</resources>
Run Code Online (Sandbox Code Playgroud)

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


sga*_*man 0

我这样做并且很幸运:

布局 res/layout/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>
Run Code Online (Sandbox Code Playgroud)

常见:res/layout/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>
Run Code Online (Sandbox Code Playgroud)