使用Android在一个框架布局中显示所有活动,碎片

时间:2014-07-08 07:06:40

标签: android android-fragments android-framelayout

我正在DrawerLayout使用Slider Menu,它是主要的活动。它包含Frame Layout

主页面为主页,将在Frame Layout中打开,且为Fragment。它工作正常。现在,Home包含2个按钮,我根据点击Activity打开不同的Button

它会打开新的Activity,但我想在Home所在的相同Frame Layout中打开它,这样我就可以在所有Activity中扩展Slider菜单。

如何实施?

home.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/lightgray"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/llPractice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_list_selector"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="5dp" >

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:contentDescription="@string/app_name"
                android:src="@drawable/practice" />

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:padding="3dip" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="5dip"
                    android:gravity="center_vertical"
                    android:text="@string/practice"
                    android:textColor="@color/dark_black"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:typeface="sans" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/arrow" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llStudy"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_list_selector"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="5dp" >

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:contentDescription="@string/app_name"
                android:src="@drawable/study" />

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:padding="3dip" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="5dip"
                    android:gravity="center_vertical"
                    android:text="@string/study"
                    android:textColor="@color/dark_black"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:typeface="sans" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/arrow" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

</ScrollView>

Home.java:

public class Home extends Fragment {

    View llPractice;
    View llStudy;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.home, container, false);

        llPractice = rootView.findViewById(R.id.llPractice);
        llStudy = rootView.findViewById(R.id.llStudy);
        llTest = rootView.findViewById(R.id.llTest);
        llGKQuiz = rootView.findViewById(R.id.llGKQuiz);

        llPractice.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent openRandomQuestion = new Intent(getActivity(),
                        Practice.class);
                startActivity(openRandomQuestion);
            }
        });

        llStudy.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                FragmentManager fm = getChildFragmentManager();
                fm.beginTransaction()
                        .replace(R.id.frame_container, new Study()).commit();
            }
        });

        llTest.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent openRandomQuestion = new Intent(getActivity(),
                    Test.class);
                startActivity(openRandomQuestion);
            }
        });


        return rootView;
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案是:

创建BaseActivity,然后在所有活动中扩展此BaseActivity:

示例代码:

        public class BaseActivity extends Activity {

            // Access you navaigation drawer here


            private DrawerLayout mDrawerLayout;
            private ListView mDrawerList;


         }

    public class YourActivity extends BaseActivity {


    }

一旦有look herehere too来创建DrawerLayout的工作演示。