片段是透明的,并在下面显示活动

Dar*_*ren 37 android fragment actionbarsherlock

我的Android应用程序启动到BeginActivity,这是SherlockFragmentActivity的子类,并显示它的第一个视图使用:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            Fragment f = LoginFragment.newInstance();

            getSupportFragmentManager()
                    .beginTransaction()
                    .add(android.R.id.content, f, "loginfragment")
                    .attach(f)
                    .commit();
        }
}
Run Code Online (Sandbox Code Playgroud)

LoginFragment显示如下视图:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.login, container, false);

        // Get pointers to text views
        usernameField = (EditText) v.findViewById(R.id.usernameLog);
        passwordField = (EditText) v.findViewById(R.id.passwordLog);
        progressBar = (ProgressBar) v.findViewById(R.id.progressBarLog);
        // Set button click listeners for both buttons
        Button b = (Button) v.findViewById(R.id.loginButton);
        b.setOnClickListener(this);

        return v;
    }
Run Code Online (Sandbox Code Playgroud)

点击登录时,我会显示如下列表视图:

BeginActivity top = (BeginActivity) getActivity();
Fragment f = OfferListFragment.newInstance();
        top.getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, f, "offerList")
                .addToBackStack(f.getClass().getSimpleName())
                .commit();
Run Code Online (Sandbox Code Playgroud)

最后,OfferListFragment显示如下视图:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.offers, container, false);

        return v;
    }
Run Code Online (Sandbox Code Playgroud)

现在我遇到的问题是,最终的OfferListFragment似乎是透明的,我可以看到它下面的登录界面.我正在使用具有黑色背景的Theme.Sherlock.我是否应该手动将视图背景设置为黑色?或者主题中的黑色是否可以由系统上的用户自定义?(我不是Android用户).

谢谢

Goo*_*ead 79

恕我直言,我不同意这个答案.

您可能想要替换您的片段,或者您可能想要添加它.

例如,假设您在一个片段中,该片段是由网络请求检索的列表,如果您使用let更改片段替换detailFragment并将其添加到backStack.

当你回去你的片段将重做网络查询,当然你可以缓存它,但为什么?它是一个回到以前的状态,所以添加最后一个片段将处于完全相同的状态,没有任何代码.

片段默认是透明的,因为它们只能用于绘制屏幕的一小部分,但如果你的片段是match_parent,那么只需将其背景设置为一种颜色,并继续在fragmentTransaction上使用add.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
Run Code Online (Sandbox Code Playgroud)

这将是片段布局XML的根元素,可以是线性的等等,事务代码是:

YourFragment detail = YourFragment.newInstance(Id);
ft.add(R.id.contentHolder, detail);
ft.addToBackStack(TAG);
ft.commit();
Run Code Online (Sandbox Code Playgroud)

希望这有助于想要知道如何在不更改添加的情况下查看实体背景的人,这通常不是最好的情况.

问候

  • 什么可能更有帮助的是在片段视图的顶部元素上设置android:background ="?android:attr/windowBackground".这样就使用了默认背景,而不是某些可能不适合您主题的白色. (24认同)
  • `碎片是默认的透明背景.仅此对我来说就足够了.我不认为我已经在文档中看到了这一点. (9认同)
  • 这确实应该是正确答案,因为前一个要求明确使用"替换"函数而不是保持与实现无关. (3认同)
  • 向根元素添加背景将抛出一个lint警告"可能的透支:根元素绘制背景".在这种情况下可以忽略它吗? (3认同)
  • 内存使用情况如何?! (2认同)

Meh*_*sar 37

尝试将FragmentTransaction类用于replace片段而不是仅添加.

说明:

每个事务都是您要同时执行的一组更改.您可以设置所有要使用的方法,如特定交易进行变化add(),remove()replace().然后,要将事务应用于活动,您必须调用commit().

commit()但是,在调用之前,您可能需要调用addToBackStack(),以便将事务添加到片段事务的后台堆栈中.此后备堆栈由活动管理,并允许用户通过按"返回"按钮返回到先前的片段状态.

例如,以下是如何将一个片段替换为另一个片段,并保留后栈中的先前状态:

例:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
Run Code Online (Sandbox Code Playgroud)

参考: 请查看管理碎片

我希望它会有所帮助!!


Sul*_*Ali 5

我使用了很多技术但没有收获最后我通过添加容器的背景或像这样的片段布局来修复它

android:background="@android:color/white"    
Run Code Online (Sandbox Code Playgroud)

您只需要在Layout或您的容器视图中添加Fragment Hope它将对您有所帮助