Monday 15 September 2014

android - The id in different layout xml must be different? -


I have two layout xml file, Main.xml, a button of Android name: id = "@ + id / BtnClose ", and about.xml also has a button named Android: id =" @ + id / btnclose ", is that OK? Thanks!

main.xml

  & lt; Relative layout 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 "& gt; & Lt; 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 / out Exit "/> & Lt; / RelativeLayout & gt;   

about.xml & lt; LinearLayout xmlns: android = "http://schemas.android.com/apk/sh/Android" xmlns: Advertisement = "http://schemas.android.com/apk/lib/com.google.ads" Android: layout_width Android: layout_height = "fill_parent" Android: paddingTop = "3dip" Android: paddingLeft = "7dip" Android: background = "@ line / boundary_i" Android: orientation = "vertical" & gt; & Lt; 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 / Marryren "/> & Lt; / LinearLayout & gt;

it can be the same

Illusion / ambiguity Avoid but it is better to have different IDs as suggested by BlackBelt.

You can set the current view for hierarchy of the activity. So if you have the same ID in the different XML layout it's okay.

If the code you have

 down <> setContentView (R.layout.main) button b = (Button) findViewById (R.id.btnClose); // start button   

You are currently visible can ViewById the hierarchy main.xml

If you have below

 < Code> setContentView (R.layout.about); Button B = (button) Find VVBIID (RID. BTN close); // initialize button   

In both the above cases, both have code containing the button with main.xml and about.xml < Close the code> @ + ID / BTNC

Assume that you get an ID @ + id / button2 in about.xml and another The button is down

  setContentView (R.layout.main); Button B = (button) Find VVBIID (R.B. button2);   

You will get NullPointerException because your current view is set to the hierarchy activity main.xml not about.xml . There is no button in the main.xml with ID button2 .

No comments:

Post a Comment