包含 XML 文件会使我的应用程序崩溃
我尝试统一我的布局,因此我想重用一些 UI 元素。如果我尝试将这些元素包含在我的 main.xml 中,应用程序就会崩溃
02-15 13:07:02.470: E/AndroidRuntime(16588): 引起原因:java.lang.RuntimeException: 二进制 XML 文件第 2 行:您必须提供 layout_width 属性。
我的 XML 看起来像
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/include_topbar"/>
</LinearLayout>
<!-- include_topbar -->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout style="@style/topbar_linearlayout">
<include layout="@layout/include_icon"/>
<include layout="@layout/include_topbar_title"/>
</LinearLayout>
</merge>
<!-- include_icon -->
<merge xmlns:android="schemas.android.com/apk/res/android">
<View
android:background="@drawable/icon"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
/></merge>
<!-- include_topbar_title -->
<merge xmlns:android="schemas.android.com/apk/res/android">
<TextView
android:gravity="center"
android:text="@string/bla"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/></merge>
<!-- topbar_linearlayout -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="topbar_linearlayout">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">58dp</item>
<item name="android:padding">5dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@drawable/gradient_background</item>
</style>
</resources>
编辑:不要使用
,那么它将起作用......
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/topbar_linearlayout">
<include layout="@layout/include_icon"/>
<include layout="@layout/include_topbar_title"/>
</LinearLayout>
<!-- include_icon -->
<View xmlns:android="schemas.android.com/apk/res/android"
android:background="@drawable/icon"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
/>
I try to unify my layouts and so i want to reuse some UI elements. If i try to include those elements in my main.xml the app crashes with
02-15 13:07:02.470: E/AndroidRuntime(16588): Caused by: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.
My XML looks like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/include_topbar"/>
</LinearLayout>
<!-- include_topbar -->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout style="@style/topbar_linearlayout">
<include layout="@layout/include_icon"/>
<include layout="@layout/include_topbar_title"/>
</LinearLayout>
</merge>
<!-- include_icon -->
<merge xmlns:android="schemas.android.com/apk/res/android">
<View
android:background="@drawable/icon"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
/></merge>
<!-- include_topbar_title -->
<merge xmlns:android="schemas.android.com/apk/res/android">
<TextView
android:gravity="center"
android:text="@string/bla"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/></merge>
<!-- topbar_linearlayout -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="topbar_linearlayout">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">58dp</item>
<item name="android:padding">5dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@drawable/gradient_background</item>
</style>
</resources>
Edit: Don't use <merge>
, then it will work...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/topbar_linearlayout">
<include layout="@layout/include_icon"/>
<include layout="@layout/include_topbar_title"/>
</LinearLayout>
<!-- include_icon -->
<View xmlns:android="schemas.android.com/apk/res/android"
android:background="@drawable/icon"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在“提供的异常”中得到了答案...“您必须提供layout_width属性。”
因此,为两个 include 语句设置layout_width 和layout_height。
You have the answer in Exception provided... "You must supply a layout_width attribute."
So, set the layout_width and layout_height for both the include statements.
没有需要指定的宽度和高度属性。
does not have width and height attribute which need to be specified.