2
我有一個標題,我必須包括在多個活動,我想處理來自同一活動按鈕的OnClickListener。我跟着這個問題Button Onclick Listener in included layouts相同的佈局和相同的onclicklisteners多個活動
但在我的情況下,它不工作。 我有common_header.xml爲:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/layout_top_bar"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:clickable="true"
android:background="@color/titlebarBackground"
>
<ImageButton
android:id="@+id/btn_menu"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="menu"
android:background="@drawable/borderless_button_unselected"
android:src="@drawable/menu"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn_account"
android:textColor="@color/titlebarForeground"
android:textSize="16sp"
android:text="@string/signin"
android:background="@drawable/transparent_signin_selector"
/>
<ImageButton
android:id="@+id/btn_account"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="menu"
android:background="@drawable/borderless_button_unselected"
android:src="@drawable/account"
/>
</RelativeLayout>
<ListView
android:id="@+id/listview_account"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/menuDivider"
android:dividerHeight="1px"
android:layout_below="@+id/layout_top_bar"
android:visibility="gone"
>
</ListView>
<ListView
android:id="@+id/listview_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/menuDivider"
android:dividerHeight="1px"
android:layout_below="@+id/layout_top_bar"
android:visibility="gone"
>
</ListView>
</merge>
而在另一個佈局我用這樣的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/appBackground" >
<com.example.myApp.MenuView
android:id="@+id/common_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
.../>
<Button
.../>
</RelativeLayout>
我MenuView類:
public class MenuView extends RelativeLayout {
private LayoutInflater inflater;
public MenuView(Context context, AttributeSet attrs) {
super(context, attrs);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.common_header, this, true);
}
}
它亙古不變的告訴我任何錯誤的應用程序運行,但common_header沒有合併在我的layout.i無法弄清楚我的錯誤在哪裏,所以請幫助。
thanx for ur help ...我試着將標籤更改爲,但問題是一樣的。它沒有顯示任何錯誤,但沒有合併。我嘗試了第二種選擇,在common_header.xml中使用標籤,在另一個xml佈局中使用 ,但問題是一樣的。 –
Dharma
嘗試inflater.inflate(R.layout.common_header,this);而不是inflater.inflate(R.layout.common_header,this,true); –
我試過這個..但問題是一樣的兄弟.... – Dharma