【发布时间】:2015-06-09 03:13:33
【问题描述】:
在以下代码中,片段是从布局 xml 动态扩展的。我面临的问题是我在旋转设备时看不到碎片。
如果我查看片段管理器,我确实看到片段存在。它们被添加和附加,但在屏幕上没有。
MenuCard extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
leftContainer = (FrameLayout) findViewById(R.id.leftContainer);
rightContainer = (FrameLayout) findViewById(R.id.rightContainer);
Fragment fragment = mFragmentManager.findFragmentById(R.id.leftFragment);
if (fragment == null) {
LayoutInflater.from(this).inflate(R.layout.left_fragment, leftContainer);
}
fragment = mFragmentManager.findFragmentById(R.id.rightFragment);
if (fragment == null) {
LayoutInflater.from(this).inflate(R.layout.right_fragment, rightContainer);
}
}
}
}
}
R.layout.left_fragment
<?xml version="1.0" encoding="utf-8"?>
<fragment 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:id="@+id/leftFragment"
android:name="com.example.LeftFragment"
tools:layout="@layout/left_fragment">
</fragment>
R.layout.right_fragment
<?xml version="1.0" encoding="utf-8"?>
<fragment 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:id="@+id/rightFragment"
android:name="com.example.LeftFragment"
tools:layout="@layout/left_fragment">
</fragment>
为 R.layout.content 添加代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/leftContainer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:background="#00AFF0"
/>
<FrameLayout
android:id="@+id/rightContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v4.widget.SlidingPaneLayout>
【问题讨论】:
-
你能分享你的 content.xml 吗?
-
看起来不错,虽然附加片段的方式有点有趣,但它应该可以工作。我注意到您在 left_fragment.xml 和 right_fragment.xml 中都指定了 LeftFragment。您确定 Fragments 本身实际上在屏幕上显示任何内容吗?你能只用带有彩色背景的片段做一个简单的测试,并确保它完全显示出来吗?
-
它们在加载时完美显示内容。旋转时的问题。 mContainerId、mView、mInnerView 都为空。但是片段是可见的、附加的和添加的:o
-
左右两边的LeftFragment只是为了简化示例
-
看看底部的帖子。我已经发布了答案。
标签: android android-fragments fragment android-fragmentactivity