簡體   English   中英

使用Android在一個框架布局中顯示所有活動和片段

[英]Display All Activities, Fragments in one Frame Layout using Android

我正在使用DrawerLayoutSlider Menu ,它是主要的Activity。 它包含Frame Layout

主頁是Home,它將在Frame Layout打開,並且是Fragment 一切正常。 現在,“主頁”包含2個按鈕,我將根據單擊的Button打開不同的“ Activity

它會打開新的Activity但我想在其中有Home的同一Frame Layout中打開它,以便可以在All 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;
    }
}

解決方案是:

要創建一個BaseActivity ,然后在所有活動中擴展此BaseActivity:

樣例代碼:

        public class BaseActivity extends Activity {

            // Access you navaigation drawer here


            private DrawerLayout mDrawerLayout;
            private ListView mDrawerList;


         }

    public class YourActivity extends BaseActivity {


    }

並且一次在這里這里也可以使用DrawerLayout創建演示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM