Android 使用布局XML中的Include指令定义的按钮的资源ID

Android 使用布局XML中的Include指令定义的按钮的资源ID,android,android-layout,android-resources,Android,Android Layout,Android Resources,我遇到一个应用程序,它的布局让我困惑 假设有两个布局XML文件,其中一个包含另一个: activity_button.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_h

我遇到一个应用程序,它的布局让我困惑

假设有两个布局XML文件,其中一个包含另一个:

activity_button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include layout="@layout/button"/>

</LinearLayout>%       
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/button" 
    android:onClick="sendMessage">    
</Button>

%       
button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include layout="@layout/button"/>

</LinearLayout>%       
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/button" 
    android:onClick="sendMessage">    
</Button>

按钮的资源ID是什么

我认为人们总是必须通过
android:ID
属性来指定ID,所以我不明白这种布局为什么有效。我还检查了
R.java
ID条目,它似乎是空的

按钮的资源ID是什么

没有

我认为人们总是必须通过
android:ID
属性来指定ID

你为什么这么想?对于大多数布局的根视图,您通常不使用此属性。(请参见问题的
LinearLayout

如果您从未期望使用id查找视图,则不需要
android:id

按钮的资源ID是什么

没有

我认为人们总是必须通过
android:ID
属性来指定ID

你为什么这么想?对于大多数布局的根视图,您通常不使用此属性。(请参见问题的
LinearLayout

如果您从未期望使用id查找视图,则不需要
android:id

android:id

不是布局xml文件中视图的必填字段。仅有两个必填字段是视图的宽度和高度

想一想,当你想要一个布局简单地显示一些标题,而不需要在活动生命周期内进行任何业务更改。对于这种需要,您不需要获取此视图id,也不需要为此视图创建id的开销

android:id

不是布局xml文件中视图的必填字段。仅有两个必填字段是视图的宽度和高度

想一想,当你想要一个布局简单地显示一些标题,而不需要在活动生命周期内进行任何业务更改。对于这种需要,您不需要获取此视图id,也不需要为此视图创建id的开销