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