片段未显示在活动中 [英] Fragment not showing in activity
本文介绍了片段未显示在活动中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有自己布局的片段,应该在其中添加片段的活动具有以下布局:
I have a fragment with its own layout, and the activity in which the fragment should be added has this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<fragment
android:name="com.myapp.MyFragment"
android:id="@+id/myfragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:layout="@layout/myfragment_layout" />
</RelativeLayout>
然后在活动课中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
FragmentManager fm = getFragmentManager();
fragment = fm.findFragmentById(R.id.myfragment);
if (fragment == null) {
fragment = new MyFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.myfragment, fragment, "myfragment");
ft.commit();
}
}
该片段的onCreateView为:
The onCreateView of the fragment is:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.myfragment, container, false);
}
但是该片段未显示.没有异常被抛出.活动保持空白.
But the fragment is not showing. No exception is thrown. The activity remains blank.
我想念什么?
推荐答案
替换以下几行
android:layout_weight ="1"android:layout_width ="0dp"
android:layout_weight="1" android:layout_width="0dp"
与
android:layout_width ="match_parent"
或 android:layout_width ="wrap_content"
因为 RelativeLayout
不接受layout_weight.
because RelativeLayout
doesn't accept the layout_weight.
这篇关于片段未显示在活动中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文