【问题标题】:Android implement CoordinatorLayout and FramelayoutAndroid实现CoordinatorLayout和Framelayout
【发布时间】:2015-12-18 13:07:03
【问题描述】:

我在我的应用程序主活动布局中使用以下代码

<?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;include layout="@layout/content_main"/&gt; 行中包含了片段布局,但它是静态方式,我如何用动态FrameLayout 解决方案替换这种静态方式? 我想以编程方式添加片段

【问题讨论】:

  • 可以,可以动态添加到framelayout,有什么问题?
  • 如果我用框架布局替换包含布局就可以了?

标签: android


【解决方案1】:

您可以尝试以下方法来执行FragmentTransaction,它在content_main 布局中替换您的Fragment。以下方法用于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

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    您需要将 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>
    

    在最后一个活动中,我正在动态替换片段

    getSupportFragmentManager().beginTransaction().replace(R.id.frgmentcontainer, new HomeScreen(), "Home").commit();
    

    折叠屏幕

    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();
    }
    

    }

    主屏幕片段

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      相关资源
      最近更新 更多