2014-01-27 2 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 не объединен в моем макете. Я не мог понять, где моя ошибка. Пожалуйста, помогите.

ответ

0

Вы переопределяете RelativeLayout, поэтому вместо ReliantLayout в качестве корневого элемента вашего макета вместо тега merge. Вы можете использовать его так же, как вы его используете.

<?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> 

Краткое объяснение о слиянии тегов, даже если это не то, что вы ищете, но и для других Googlers, что это может помочь:

Объединить теги используются для повторного использования файлов разметки и должны использоваться как корень файла, который затем используется из другого файла макета с включаемым тегом:

<include layout="@layout/file_name_of_file_with_merge_root"> 

Таким образом макеты слиты и иерархия сведена к минимуму, но нажмите слушатель не использовать повторно.

+0

thanx for ur help ... Я попытался изменить тег на , но проблема такая же. Он не показывает мне никаких ошибок, но не сливается. И я попробовал второй вариант, используя тег в common_header.xml и в другом макете xml, но проблема такая же. – Dharma

+0

try inflater.inflate (R.layout.common_header, это); вместо inflater.inflate (R.layout.common_header, это, правда); –

+0

Я пробовал это .. но проблема в том же брате .... – Dharma

Смежные вопросы