@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用户。)
谢谢。
OfferListFragment
时,彻底将LoginFragment
从BeginActivity
中移除? - Mehul Joisarreplace(int, android.app.Fragment)
方法了吗? - Mehul Joisarreplace
方法之后,您需要使用FragmentTransaction
类的addToBackStack
方法,最后在结尾处使用commit
方法。 - Mehul Joisar