片段是透明的,示出了下面的活动(Fragment is transparent and shows

2019-09-03 12:26发布

我的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();
        }
}

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

点击登录时,我表现出这样的列表视图:

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

最后,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;
    }

现在我遇到的问题是,最终OfferListFragment似乎是透明的,我可以看到下方的登录屏幕。 我使用的是有一个黑色的背景Theme.Sherlock。 我应该手动设置视图的背景的黑也? 或将在主题黑色是由系统上的用户自定义的? (我不是一个Android用户)。

谢谢

Answer 1:

尝试使用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();

参考:请看看管理片段

我希望这会有所帮助!



Answer 2:

恕我直言,我不同意这个答案达成一致。

您可能需要更换您的片段,或者你可能需要添加它。

例如,让我们说,你是在为通过网络请求检索,如果你替换假设的detailFragment,你把它添加到堆栈中的片段列表中的片段。

当你回到你的片段将重做网络查询,当然你可以缓存,但为什么呢? 这是一个回到以前的状态,因此与添加的最后片段将位于完全相同,没有任何代码相同的状态。

片段透明转到后台默认情况下,因为它们可以用于油漆只有屏幕的一小部分,但如果你的片段是match_parent那么就设置它的背景颜色和继续使用添加的fragmentTransaction。

<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" >

这将是对片段布局XML根元素,可以是线性的,等等等等,交易代码为:

YourFragment detail = YourFragment.newInstance(Id);
ft.add(R.id.contentHolder, detail);
ft.addToBackStack(TAG);
ft.commit();

希望这可以帮助别人,想知道怎么看一个坚实的背景不改变加,以取代通常不是最好的情况。

问候



Answer 3:

我用许多技术,但没有增益最后我通过将容器的背景或片段布局这样固定它

android:background="@android:color/white"    

你只需要在你的布局或你的容器视图中添加片段希望这将有助于



文章来源: Fragment is transparent and shows Activity below