2014-01-27 65 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

相关问题