您的位置:首页 > 产品设计 > UI/UE

<merge /> can be used only with a valid ViewGroup root and attachToRoot=true

2013-06-08 17:48 4741 查看
2down
votefavorite

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>


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?



android
 android-layout android-fragments
share|edit
edited Feb
13 at 16:02




JJD
3,73922153

asked Jul
11 '12 at 7:55





user1517039
213

  


3 Answers

activeoldestvotes

up
vote1down
vote
Apparently as stated here it
is not possible.

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


where the layout is :
<!-- 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>


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


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>


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

在<merge>中无法使用inflater.infata();;;; 

把你的布局换为Relatilayout 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐