Android 并排显示片段

Android 并排显示片段,android,android-fragments,Android,Android Fragments,我想在横向模式下为平板电脑开发一个Android应用程序,包含2个片段: 片段1包含一个按钮列表 片段2是按钮相关片段的一个空格 对于“按钮相关片段”,我的意思是:当用户按下片段1中的按钮时,与该按钮相关的片段应该显示在片段2中 片段1是一种导航面板 为了实现这一点,我编写了以下代码: <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label=

我想在横向模式下为平板电脑开发一个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>
    
    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"/>
    
    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);
      }
    
    }
    
    此代码导致以下结果:

  • 启动应用程序时,屏幕上有4个按钮(来自
    mainfrag.xml
  • 当我按下
    intro_按钮
    simulation_按钮
    时,视图分别变为
    IntroFragment
    SimulationFragment
  • 当我按下后退按钮时,这四个按钮再次可见
  • 我的问题:当
    IntroFragment
    SimulationFragment
    可见时,按钮面板消失


    我应该如何修改我的代码,使按钮面板和相应的“细节”视图(
    IntroFragment
    SimulationFragment
    )同时可见(有足够的屏幕空间)?

    此类示例显示在Android SDK中的APIDemos中。其中,左侧面板为listview,所选项目在右侧显示相关片段

    你可以在书中找到

    SDK\android-SDK\U r11-windows\android SDK windows\samples\android-14\ApiDemos

    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;
      }
    
      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);
      }
    
    }
    
    <?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>