问题描述
我使用低于code在我的应用程序的主活动布局
I am using below code in my app main activity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.keyhan_soft.gps.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
正如你看到我列入&LT片段布局,包括布局=@布局/ content_main/&GT;
行,但它是静态的方式,我怎么能取代动态的FrameLayout
解决这种静态的方式?
我想以编程方式添加片段
As you see i included Fragment layout in <include layout="@layout/content_main"/>
line, but it is static way, how can i replace this static way with dynamic FrameLayout
solution?I want to add fragments programmatically
推荐答案
您可以尝试下面的方法做 FragmentTransaction
这取代你的片段
在 content_main
layout.Below方法使用了 FragmentTransaction
。
You may try following way to do FragmentTransaction
which replace your Fragment
in content_main
layout.Below method use for FragmentTransaction
.
方法:
public void pushFragments(Fragment fragment, boolean shouldAdd, String tag) {
FragmentManager manager = getSupportFragmentManager();//Use this line for FragmentManager , if you are in Activity
//FragmentManager manager = getActivity().getSupportFragmentManager();//Use this line for FragmentManager , if you are in Fragment
FragmentTransaction ft = manager.beginTransaction();
//manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);//Uncomment this line if you don't want to maintain Fragment Backsack
ft.replace(R.id.content_main, fragment, tag);
if (shouldAdd) {
ft.addToBackStack(tag); //add fragment in backstack
}
ft.commit();
}
我给例子怎么办 FragmentTransaction
。
YourFragment.java
public class YourFragment extends BaseFragment {
private View rootView;
public YourFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.layout_your_fragment, container, false);
return rootView;
}
}
示例:
pushFragments(new YourFragment(),
true, // true --> add Fragment in backstack else false
"YourFragment"); // Fragment tag which help to find your fragment
我希望它的你的帮助。
I hope its help you.
这篇关于Android的实施CoordinatorLayout和的FrameLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!