在FragmentActivity中替换Fragment期间优化布局

时间:2013-03-21 19:09:18

标签: android

我尝试在Fragment运行时替换FragmentActivity

fragment_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MyFragmentActivity.java

public class MyFragmentActivity extends SlidingFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity);
    }

    // Will be called by AsyncTaskLoader's onLoadFinished.
    public void selectActiveContent() {
        // MyFragment's top level is LinearLayout
        Fragment fragment = new MyFragment();         

        getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commitAllowingStateLoss();
    }

但是,我意识到结果没有得到优化。

enter image description here

我意识到通过我自己FrameLayout提供的fragment_activity.xml正在附加到另一位父FrameLayout

而不是

FrameLayout
    FrameLayout
        LinearLayout
            LinearLayout
            ListView

我希望

FrameLayout
    LinearLayout
        LinearLayout
        ListView

我可以知道如何实现这一目标吗?是否有可能fragment_activity.xml没有ViewGroup(FrameLayout)?

1 个答案:

答案 0 :(得分:0)

虽然<merge>元素专门用于删除无用级别的视图层次结构,但每个this answer,您不能在Fragments中使用合并标记,而您必须使用ViewGroup根视图,如您所注意到的,最轻的权重(即,最快的渲染)是FrameLayout。