我在我的应用主要活动布局中使用以下代码
<?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>
如您所见,我在<include layout="@layout/content_main"/>
行中包含片段布局,但它是静态方式,如何用动态FrameLayout
解决方案替换此静态方式?
我想以编程方式添加片段
答案 0 :(得分:2)
您可以尝试按以下方式执行FragmentTransaction
替换Fragment
布局中的content_main
。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
我希望它对你有所帮助。
答案 1 :(得分:1)
您需要将Framelayout作为容器提供动态提供片段。
布局文件
collapse_screen_test.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frgmentcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appbar"></FrameLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@android:drawable/ic_dialog_email"
app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>
在Activity中,我最后动态替换片段
getSupportFragmentManager().beginTransaction().replace(R.id.frgmentcontainer, new HomeScreen(), "Home").commit();
<强> CollpaseScreen 强>
public class CollpaseScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collapse_screen_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_launcher);
replace(new HomeScreen(), false, "Home");
}
public void replace(Fragment fragment, boolean addtostack, String tag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frgmentcontainer, fragment, tag);
if (addtostack)
ft.addToBackStack(tag);
ft.commit();
}
}
HomeScreen Fragment
public class HomeScreen extends Fragment{
public static final String TAG = "Home";
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_screen, container, false);
return v;
}
}
home_screen 片段布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp">
<TextView
android:layout_width="fill_parent" android:id="@+id/htmltext"
android:layout_height="match_parent" />
</RelativeLayout>