片段是透明的,并在下面显示Activity [英] Fragment is transparent and shows Activity below
问题描述
@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);
//膨胀这个片段的布局
View v = inflater.inflate(R.layout.login,container,false);
//获取指向文本视图的指针
usernameField =(EditText)v.findViewById(R.id.usernameLog);
passwordField =(EditText)v.findViewById(R.id.passwordLog);
progressBar =(ProgressBar)v.findViewById(R.id.progressBarLog);
//设置按钮点击这两个按钮的监听器
按钮b =(按钮)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查看onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){
//膨胀此片段的布局
View v = inflater.inflate(R.layout.offers,container,false);
return v;
$ / code>
现在我遇到的问题是最终的OfferListFragment似乎是透明的我可以看到它下面的登录屏幕。我使用的是具有黑色背景的Theme.Sherlock。我应该手动将视图背景设置为黑色吗?或者主题中的黑色可以由系统上的用户自定义? (我不是Android用户)。
$ b
谢谢 >尝试使用 FragmentTransaction
类到替换
碎片,而不是仅仅添加。
说明:
每笔交易都是您想要同时执行的一系列更改。您可以使用诸如 add()
, remove()$ c $等方法设置您要为给定事务执行的所有更改c>和
replace()
。然后,要将事务应用于该活动,必须调用 commit()
。
在调用 commit()
,但是,您可能需要调用 addToBackStack()
,以便将事务添加到片段交易。这个后退堆栈由活动管理,并允许用户通过按返回按钮返回到前一个片段状态。
例如,以下是如何替换一个与另一个片段相结合,并将前一个状态保留在后退堆栈中:
示例:
//创建新的片段和事务
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager()。beginTransaction();
//将fragment_container视图中的任何内容替换为此片段
//并将该事务添加到后端堆栈
transaction.replace(R.id.fragment_container,newFragment );
transaction.addToBackStack(null);
//提交交易
transaction.commit();
参考:
请看管理片段
I希望它会有帮助!!
My Android application launches into BeginActivity which is a subclass of SherlockFragmentActivity and shows it's first view using:
@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 shows a view like this:
@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;
}
when clicking login I show a list view like this:
BeginActivity top = (BeginActivity) getActivity();
Fragment f = OfferListFragment.newInstance();
top.getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, f, "offerList")
.addToBackStack(f.getClass().getSimpleName())
.commit();
and finally, OfferListFragment displays its view like this:
@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;
}
Now the problem I am having, is that the final OfferListFragment seems to be transparent and I can see the login screen below it. I am using Theme.Sherlock that has a black background. Should I be manually setting the views backgrounds to black also? Or would the black in the theme be customisable by the user on the system? (I'm not an Android user).
Thanks
Try using FragmentTransaction
class to replace
the fragments instead of just adding.
Explanation:
Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add()
, remove()
, and replace()
. Then, to apply the transaction to the activity, you must call commit()
.
Before you call commit()
, however, you might want to call addToBackStack()
, in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.
For example, here's how you can replace one fragment with another, and preserve the previous state in the back stack:
Example:
// 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();
Reference: Please have a look at Managing Fragments
I hope it will be helpful !!
这篇关于片段是透明的,并在下面显示Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!