相同的布局和相同onclicklisteners多个活动 [英] same layout and same onclicklisteners for multiple activities
问题描述
我有一个头,我在多个活动,包括和我要处理的OnClickListener从同一活动的按钮。我跟着这个问题按钮的onClick监听器在包括布局
I have a header that i have to include in multiple activities and i want to handle the OnClickListener for the Buttons from same activity. i followed this question Button Onclick Listener in included layouts
但在我的情况下,它亘古不变的工作。
我已经common_header.xml为:
but in my case it doesnot work. i have common_header.xml as:
<?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>
和在另一个布局我已经用它是这样的:
and in another layout i have used it like this:
<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类是:
my MenuView class is:
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合并couldnot找出何处是我mistake.so请帮助。
it doesnot show me any error the app runs but the common_header is not merged in my layout.i couldnot figure out where is my mistake.so please help.
推荐答案
您要重写RelativeLayout的所以只是有一个RelativeLayout的作为布局的根元素,而不是合并标签。
然后,您可以根据您使用的是它使用它而已。
You are overriding RelativeLayout so just have a RelativeLayout as the root element of your layout instead of the merge tag. You can then use it just as you are using it.
<?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,这可能帮助:
A short explanation about merge tags, even though it is not what you are looking for, but for other googlers that this may help:
合并标记用于重用布局文件和应作为文件的根目录。然后,从一个不同的布局文件中使用与包括标记:
Merge tags are used for reusing layout files and should be used as root of file that is then used from a different layout file with an include tag:
<include layout="@layout/file_name_of_file_with_merge_root">
这样的布局合并,层次结构被最小化,但点击听众都不要再用。
This way the layouts are merged and the hierarchy is minimized but click listeners are not reused.
这篇关于相同的布局和相同onclicklisteners多个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!