我正在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;
}
}
答案 0 :(得分:0)