android-主详细信息中的变量mTwoPane不';不变

android-主详细信息中的变量mTwoPane不';不变,android,android-layout,master-detail,android-orientation,Android,Android Layout,Master Detail,Android Orientation,我试图使用masterDetail布局,尽可能地改变负数,试图理解它们的默认结构 我正在我的三星S5上尝试应用程序 应用程序正常工作,但在横向模式下,不会在布局的右侧添加片段 我认为问题在于变量mTwoPane没有改变值,保持为false,这是因为findViewById(R.id.pokemon\u detail\u container)!=空显然永远是假的 代码如下: onCreate in Main活动中的块: if (findViewById(R.id.detail_co

我试图使用masterDetail布局,尽可能地改变负数,试图理解它们的默认结构

我正在我的三星S5上尝试应用程序

应用程序正常工作,但在横向模式下,不会在布局的右侧添加片段

我认为问题在于变量mTwoPane没有改变值,保持为false,这是因为findViewById(R.id.pokemon\u detail\u container)!=空显然永远是假的

代码如下:

onCreate in Main活动中的块:

        if (findViewById(R.id.detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-w900dp).
        // If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;
    }
        public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setImageResource(mValues.get(position).id);
        holder.mNameView.setText(mValues.get(position).name);


        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mTwoPane) Log.d("mTwoPane", "is true");
                else Log.d("mTwoPane", "is false");
                if (mTwoPane) {
                    Bundle arguments = new Bundle();
                    arguments.putInt(DetailFragment.ARG_ITEM_ID, holder.mItem.id);
                    DetailFragment fragment = new DetailFragment();
                    fragment.setArguments(arguments);
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.detail_container, fragment)
                            .commit();
                } else {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra(DetailFragment.ARG_ITEM_ID, holder.mItem.id);

                    context.startActivity(intent);
                }
            }
        });
    }
注释是默认的

activity_detail.xml中的detail_conter:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.soissy.masterdetail.DetailActivity"
tools:ignore="MergeRootFrame">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

我不明白如何添加片段(mTwoPane=true)

您可能没有选择您认为的布局。确保您遵循本文档中关于如何操作的说明。测试Android是否正在选择您期望的布局的一个简单方法是更改该布局文件中的某些内容,并且只有该文件在显示时对您来说是显而易见的,例如颜色或文本。如果你看不到变化,那么你就看不到你认为自己是的布局。

我解决了问题。当您在开始时使用Android Studio创建MasterDetail“活动”时,它会自动为双窗格模式创建布局。然而,我认为只适用于平板电脑,因为这种布局的具体选项是“w900dp”。所以我为景观模式创建了另一个布局。然后从布局“with w900dp”复制并粘贴到我的横向布局,现在一切正常。

我也有同样的问题,布尔值始终为false,但在我的情况下,我将适配器作为一个单独的java文件,因此在我的列表片段中实现了一个实例callbck,然后我将

如果找到VIEWBYID R.id.detail\U容器=无效的 mTwoPane=true

进入If(mTwopane)之前的onclick回调。现在我可以在“风景”和“人像”中查看两个窗格。细节活动生效,因此检查视图是否为!=null没有运行,这就是布尔值保持为false的原因我希望这对某些人有所帮助