我想开发用于在横向模式下的片剂Android应用与2个片段:显示片段并排侧
- 片段1包含按钮的列表。
- 片段2是一个按钮相关片段的空间。
随着“按钮相关的片段”我的意思如下:当用户在片段1按下按钮,与该按钮相关联的片段应片段被显示2.
片段1是种导航面板。
为了实现这一点,我写了下面的代码:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="mypackage.MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".impl.activities.IntroActivity"></activity>
<activity android:name=".impl.activities.SimulationActivity"></activity>
</application>
MainActivity
看起来是这样的:
public class MainActivity extends Activity implements IMainActivity {
@Override
public void onCreate(final Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
setContentView(R.layout.main);
}
@Override
public void show(final View aView, final Class<? extends Activity> aActivityClass) {
startActivity(new Intent(this, aActivityClass));
}
}
main.xml
有以下内容:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="mypackage.fragments.ContentFragment"/>
ContentFragment
看起来是这样的:
public class ContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.mainfrag, container, false);
IMainActivity mainActivity = (IMainActivity) this.getActivity();
final IntroButtonClickListener introListener = new IntroButtonClickListener(
mainActivity, IntroActivity.class);
final IntroButtonClickListener simulationListener = new IntroButtonClickListener(
mainActivity, SimulationActivity.class);
view.findViewById(R.id.intro_button).setOnClickListener(introListener);
view.findViewById(R.id.simulation_button).setOnClickListener(simulationListener);
return view;
}
IntroButtonClickListener
:
class IntroButtonClickListener implements View.OnClickListener {
private IMainActivity mainActivity;
private Class<? extends Activity> activityClass;
public IntroButtonClickListener(final IMainActivity aMainActivity,
final Class<? extends Activity> aActivityClass) {
this.mainActivity = aMainActivity;
this.activityClass = aActivityClass;
}
@Override
public void onClick(final View aView) {
this.mainActivity.show(aView, this.activityClass);
}
}
mainfrag.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="@+id/intro_button"
android:text="@string/intro_button"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="@+id/simulation_button"
android:text="@string/simulation_button"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="@+id/statistics_button"
android:text="@string/statistics_button"/>
<Button
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="25"
android:id="@+id/help_button"
android:text="@string/help_button"/>
</LinearLayout>
此代码将导致以下结果:
- 当我启动应用程序,也有在屏幕上的4个按钮(从
mainfrag.xml
)。 - 当我按
intro_button
或simulation_button
时,视图分别变为IntroFragment
或SimulationFragment
。 - 当我按下后退按钮时,四个按钮再次可见。
我的问题:当IntroFragment
或SimulationFragment
变得可见时,按钮面板消失。
我应该如何修改我的代码,这样既按钮面板和各自的“细节”视图(IntroFragment
,SimulationFragment
)是在同一时间看到(有足够的屏幕空间这个)?