2012-09-17 167 views
4

我尝试设置这是我的活动的布局:包括不工作

<?xml version="1.0" encoding="utf-8"?> 
<include layout="@layout/map_activity_base" /> 

如果我使用@layout/map_activity_base,而不是包括它的,它的工作原理。为什么我不这样做是因为你不能在小屏幕布局中包含小屏幕布局的内容,但我需要在不同的布局中使用@layout/map_activity_base的内容。

我得到的错误是

E/AndroidRuntime(11383): FATAL EXCEPTION: main 
E/AndroidRuntime(11383): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.l_one.app.achileo/de.l_one.app.achileo.Main}: android.view.InflateException: Binary XML file line #2: Error inflating class include 
[...] 
E/AndroidRuntime(11383): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class include 
[...] 
E/AndroidRuntime(11383): Caused by: java.lang.ClassNotFoundException: android.view.include in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/de.l_one.app.achileo-1.apk] 

如此看来,Android的自以为<include />应该是一个View但只有发生,如果我用<include />没有任何周边View

因此,它是可能的使用<include />没有任何围绕View,如果不是什么是实现我想要的最佳方式?

顺便说一句:我不知道它是否重要,但这是一个图书馆项目的内容。

编辑:map_activity_base.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mapContainer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView android:id="@+id/listHeader" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:gravity="center" 
    android:text="@string/listHeader" 
    android:padding="5dp" /> 

<View android:id="@+id/divider" 
    android:layout_width="100dp" 
    android:layout_height="1dp" 
    android:background="#333" 
    android:layout_below="@id/listHeader" /> 

<de.l_one.app.map.base.POIListView android:id="@+id/poiList" 
    android:layout_width="100sp" 
    android:layout_height="match_parent" 
    android:divider="#cccccc" 
    android:dividerHeight="1dp" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@id/divider" 
    android:listSelector="@android:color/transparent" /> 

<com.google.android.maps.MapView 
    android:id="@+id/mapview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_toRightOf="@id/poiList" 
    android:clickable="true" 
    android:apiKey="@string/googleAPIKey" /> 

<TextView android:id="@+id/cmtryNameTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:padding="5dp" 
    android:background="@drawable/cmtry_txt_view_back" /> 

<TextView android:id="@+id/navigate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_below="@id/cmtryNameTextView" 
    android:padding="5dp" 
    android:background="@drawable/cmtry_txt_view_back" 
    android:visibility="gone" 
    android:text="@string/navigate" /> 

</RelativeLayout> 

回答

2

我相信你需要把 “包括” 根视图内至少。像以下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
... 

    <include layout="@layout/titlebar"/> 

... 

/>

1

它看起来像<include/>不能在布局的根来定义。如果你真的需要开始与包括一个更好的办法是使用<merge/>

文件fragment_map.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    the <merge/> tag must be the root element 
    while it actually defines an xml include. 
--> 
<merge 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- insert some layout here --> 

</merge> 

文件activity_map.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- then the <include/> tag can then reference it, while merging. --> 
    <include 
     layout="@layout/fragment_map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</android.support.design.widget.CoordinatorLayout> 
+0

更新XML /注释一点点,因为我挣扎(并得到了这样的合并工作)。 –