2014-01-27 134 views
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無法弄清楚我的錯誤在哪裏,所以請幫助。

回答

0

您正在重寫RelativeLayout,因此只需將RelativeLayout作爲佈局的根元素而不是合併標記。 然後,您可以像使用它一樣使用它。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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> 
</RelativeLayout> 

有關合並標籤的簡短說明,即使它是不是你在找什麼,但對於其他的Google,這可能幫助:

合併標籤用於重用佈局文件,應當使用作爲文件的根目錄,然後從不同的佈局文件用於與包括標籤:

<include layout="@layout/file_name_of_file_with_merge_root"> 

這樣的佈局被合併和層次最小化,但點擊聽衆都不要再用。

+0

thanx for ur help ...我試着將標籤更改爲,但問題是一樣的。它沒有顯示任何錯誤,但沒有合併。我嘗試了第二種選擇,在common_header.xml中使用標籤,在另一個xml佈局中使用,但問題是一樣的。 – Dharma

+0

嘗試inflater.inflate(R.layout.common_header,this);而不是inflater.inflate(R.layout.common_header,this,true); –

+0

我試過這個..但問題是一樣的兄弟.... – Dharma

相關問題