gpt4 book ai didi

android - fragment 是透明的,并在下面显示 Activity

转载 作者:IT王子 更新时间:2023-10-29 00:10:19 29 4
gpt4 key购买 nike

我的 Android 应用程序启动到 BeginActivity,它是 SherlockFragmentActivity 的子类,并使用以下方法显示它的第一个 View :

@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 显示如下 View :

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

点击登录时,我会显示如下 ListView :

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

最后,OfferListFragment 显示如下 View :

@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。我也应该手动将 View 背景设置为黑色吗?或者主题中的黑色是否可以由系统上的用户自定义? (我不是 Android 用户)。

谢谢

最佳答案

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

您可能想要替换您的 fragment ,或者您可能想要添加它。

例如,假设您在一个 fragment 中,该 fragment 是由网络请求检索的列表,如果您将 fragment 替换为例如 detailFragment 并将其添加到 backStack。

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

默认情况下,Fragment 是透明背景的,因为它们只能用于绘制屏幕的一小部分,但如果您的 Fragment 是 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" >

这将是 fragment 布局 XML 上的根元素,可以是线性的,等等交易代码是:

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

希望这可以帮助那些想知道如何在不更改添加替换的情况下看到纯色背景的人,这通常不是最好的情况。

问候

关于android - fragment 是透明的,并在下面显示 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16575177/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com