本文介绍了Android:Merge-Tag 不适用于片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用片段的初学者.是否可以有这样的布局:

I'm a beginner in using fragments. Is it possible to have a layout like this:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <include layout="@layout/waiting_dialog"/>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@drawable/bg_tile"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:visibility="invisible">

        <include layout="@layout/no_entries"/>
    </LinearLayout>
</merge>

并在片段的 onCreateView 中使用它,如下所示:

and use it in the fragments' onCreateView like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main, container, false);
    return view;
}

总是出现以下错误:

07-11 09:53:47.608: E/AndroidRuntime(5602): android.view.InflateException: \
  <merge /> can be used only with a valid ViewGroup root and attachToRoot=true

如何处理这个问题?

推荐答案

显然如此处所述,这是不可能的.

Apparently as stated here it is not possible.

我所做的是完全删除根布局"(参见battery_details.xml).所以我为我的 MonitorActivity 创建了一个片段布局:

What I did was altogether drop the "root layout" (see battery_details.xml). So I create a fragment layout for my MonitorActivity :

public class MonitorActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor);
    }
}

布局在哪里:

<!-- activity_monitor.xml -->
<LinearLayout 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"
    android:baselineAligned="false"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MonitorActivity" >
    <fragment android:name="di.k23b.hw3.fragments.MonitorDetailsFragment"
            android:id="@+id/monitor_details"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="di.k23b.hw3.fragments.MonitorPrefsFragment"
            android:id="@+id/monitor_preferences"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

在 MonitorDetailsFragment 类中:

and in MonitorDetailsFragment class :

public class MonitorDetailsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.battery_details, container, false); // NEVER TRUE !
    }
}

其中 battery_details.xml(在 res/layout 中创建):

where the battery_details.xml (created in res/layout) :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/batteryTextHealth"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="BATTERRRYYY"
    android:textIsSelectable="true" >
</TextView>

作品.所以也许你可以编辑你使用片段的布局,在那里添加 并将 的元素直接放入你使用的布局中在 OnCreateView - 删除合并标签

Works. So maybe you can edit the layout you use the fragments, add the <LinearLayout> there and just put the elements of the <LinearLayout> directly into the layout you use in the OnCreateView - dropping the merge tags

这篇关于Android:Merge-Tag 不适用于片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:02