Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 不同布局xml中的id必须不同?_Android - Fatal编程技术网

Android 不同布局xml中的id必须不同?

Android 不同布局xml中的id必须不同?,android,Android,我有两个布局xml文件,在Main.xml中有一个名为android=“@+id/btnClose”的按钮,在About.xml中还有一个名为android:id=“@+id/btnClose”的按钮, 可以吗?谢谢 Main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:lay

我有两个布局xml文件,在Main.xml中有一个名为android=“@+id/btnClose”的按钮,在About.xml中还有一个名为android:id=“@+id/btnClose”的按钮, 可以吗?谢谢

Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="3dip"
    android:layout_marginTop="3dip"
    android:background="#DCDCDC" >

     <Button
        android:id="@+id/btnClose"
        style="@style/myTextAppearance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/exit" />
</RelativeLayout>

About.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"   
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="3dip"
    android:paddingLeft="7dip"
    android:background="@drawable/border_ui"
    android:orientation="vertical" >

     <Button
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        style="@style/myTextAppearance"
        android:text="@string/myreturn" />


</LinearLayout>

不,这不是强制性的
findViewById()
引用当前的“视图”层次结构。Imo最好避免歧义

否,这不是强制性的
findViewById()
引用当前的“视图”层次结构。在我看来,最好避免歧义

它可以是相同的

但为了避免混淆/歧义,最好按照blackbelt的建议使用不同的ID

您可以
findViewById
将当前视图层次结构设置为活动。所以,如果您在不同的xml布局中有相同的ID,就可以了

如果您有以下信息:

setContentView(R.layout.main)
Button b = (Button) findViewById(R.id.btnClose); // initialize button
 setContentView(R.layout.main);
 Button b = (Button) findViewById(R.id.button2);
您可以找到当前视图层次结构的ViewById。在您的例子中,main.xml

如果您有以下信息:

setContentView(R.layout.about);
Button b = (Button) findViewById(R.id.btnClose); // initialize button
上述两种情况都有效,因为
main.xml
about.xml
都有id为
@+id/btncClose的按钮

假设您在
about.xml
中有第二个id为
@+id/button2
的按钮,并且您有以下内容

setContentView(R.layout.main)
Button b = (Button) findViewById(R.id.btnClose); // initialize button
 setContentView(R.layout.main);
 Button b = (Button) findViewById(R.id.button2);
您将获得
NullPointerException
,因为设置为活动的当前视图层次结构是
main.xml
而不是
about.xml
main.xml
没有id为
button2

的按钮,它可以是相同的

但为了避免混淆/歧义,最好按照blackbelt的建议使用不同的ID

您可以
findViewById
将当前视图层次结构设置为活动。所以,如果您在不同的xml布局中有相同的ID,就可以了

如果您有以下信息:

setContentView(R.layout.main)
Button b = (Button) findViewById(R.id.btnClose); // initialize button
 setContentView(R.layout.main);
 Button b = (Button) findViewById(R.id.button2);
您可以找到当前视图层次结构的ViewById。在您的例子中,main.xml

如果您有以下信息:

setContentView(R.layout.about);
Button b = (Button) findViewById(R.id.btnClose); // initialize button
上述两种情况都有效,因为
main.xml
about.xml
都有id为
@+id/btncClose的按钮

假设您在
about.xml
中有第二个id为
@+id/button2
的按钮,并且您有以下内容

setContentView(R.layout.main)
Button b = (Button) findViewById(R.id.btnClose); // initialize button
 setContentView(R.layout.main);
 Button b = (Button) findViewById(R.id.button2);

您将获得
NullPointerException
,因为设置为活动的当前视图层次结构是
main.xml
而不是
about.xml
main.xml
没有id为
button2

的按钮。可以,因为两个id都在不同的xml中。但是,如果您在编码中经常使用这些id,您自己会在某些时候感到困惑。因此,使用不同的id是很好的,例如
@+id/btnCloseMain
@+id/btnCloseAbout

是的,这很好,因为两个id都在不同的xml中。但是,如果您在编码中经常使用这些id,您自己会在某些时候感到困惑。因此,有不同的id是很好的,例如
@+id/btnCloseMain
@+id/btnCloseAbout

是的,它会工作得很好…是的,它会工作得很好。。。