安卓:合并,标签不工作的碎片 [英] Android: Merge-Tag doesn't work with fragments

查看:136
本文介绍了安卓:合并,标签不工作的碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用碎片初学者。是否有可能有这样的布局:

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;
}

总是有以下错误:

There's always the following error:

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

如何处理这个问题呢?

How to handle this problem?

推荐答案

显然是说这里这是不可能的。

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 /布局创建):

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

作品。因此,也许您可​​以编辑您使用片段布局,添加&LT;的LinearLayout&GT; 那里,只是把&LT元素;的LinearLayout&GT; 直接进入您在使用布局 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

这篇关于安卓:合并,标签不工作的碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆