Android layout file
fI have created two layout files. First one is main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<ImageButton android:src="@drawable/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/addBtn" ></ImageButton>
<ImageButton android:src="@drawable/remove" android:layout_width="wrap_content" android:layout_height="wrap_content" andro开发者_StackOverflowid:id="@+id/remBtn"></ImageButton>
</LinearLayout>
<ListView android:layout_height="wrap_content" android:id="@+id/myListView" android:layout_width="match_parent"
android:background="#676767" >
</ListView>
<Button android:text="Add" android:id="@+id/dialogAddBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
</LinearLayout>
and the second one is dialog.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<EditText android:id="@+id/dialogEditText" android:text="Enter Your Text Here" android:layout_height="wrap_content" android:layout_width="match_parent"></EditText>
<DatePicker android:id="@+id/datPicker" android:layout_height="wrap_content" android:layout_width="match_parent" ></DatePicker>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:text="Add" android:id="@+id/dialogAddBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
<Button android:text="Cancel" android:id="@+id/dialogCancelBtn" android:layout_width="200px" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
</LinearLayout>
</LinearLayout>
If u look closely than u will find that "dialogAddbtn" is in both layouts. Now , How can i distinguish those buttons in java coding. Becoz both buttons have same id.
Add id tags to both the layouts, Then using inflater, do something similar to this
LayoutInflater inflater = LayoutInflater.from(this);
inflatedView1 = inflater.inflate(R.layout.layout1,null);
inflatedView2 = inflater.inflate(R.layout.layout2,null);
Button button1 = (Button)inflatedView1.findViewbyId(R.id.dialogAddBtn);
Button button2 = (Button)inflatedView2.findViewbyId(R.id.dialogAddBtn);
As you can see both the buttons can be accessed, as both are in different parent views.
精彩评论