包含 XML 文件会使我的应用程序崩溃

发布于 2025-01-06 09:22:20 字数 2600 浏览 3 评论 0原文

我尝试统一我的布局,因此我想重用一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2025-01-13 09:22:20

您在“提供的异常”中得到了答案...“您必须提供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.

虐人心 2025-01-13 09:22:20
<LinearLayout style="@style/topbar_linearlayout">

没有需要指定的宽度和高度属性。

<LinearLayout style="@style/topbar_linearlayout">

does not have width and height attribute which need to be specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文