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)
希望这有助于想要知道如何在不更改添加的情况下查看实体背景的人,这通常不是最好的情况.
问候
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)
参考: 请查看管理碎片
我希望它会有所帮助!!
我使用了很多技术但没有收获最后我通过添加容器的背景或像这样的片段布局来修复它
android:background="@android:color/white"
Run Code Online (Sandbox Code Playgroud)
您只需要在Layout或您的容器视图中添加Fragment Hope它将对您有所帮助
| 归档时间: |
|
| 查看次数: |
49559 次 |
| 最近记录: |