我想为具有 2 个片段的横向模式下的平板电脑开发一个 Android 应用程序:
- 片段 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
)同时可见(有足够的屏幕空间)?