【问题标题】:Unable to have two pane mode for tablet in android无法在 android 中为平板电脑设置两个窗格模式
【发布时间】:2016-05-14 14:14:04
【问题描述】:

我正在尝试为我尝试在模拟器 (Nexus 10) 上测试应用程序的 android 平板电脑显示两个窗格。我为此创建了一个activity_man.xml(在文件夹layout-sw600dp 中)。它包含一个专属于它的FrameLayoutandroid:id="@+id/movie_detail_container",如下所述。

  activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    tools:context="akshit.android.com.popularmovies.MainActivity">

    <!--
    This layout is a two-pane layout for the Items master/detail flow.
    -->

    <fragment
        android:id="@+id/fragment_movie_main"
        android:name="akshit.android.com.popularmovies.MainActivityFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/movie_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4" />

</LinearLayout>

基于此,我检查了如果视图包含 id,我应该有mTwoPane = true 以及启动DetailFragment(以支持movie_details_container)。但是mTwoPane 仍然是假的。

public class MainActivity extends AppCompatActivity {
    private static final String DETAILFRAGMENT_TAG = "DFTAG";

    boolean mTwoPane = false;
    public static String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (findViewById(R.id.movie_detail_container) != null) {
            mTwoPane = true;
            Log.i(TAG, "mTwoPane value=" + mTwoPane);
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.movie_detail_container, new DetailsActivityFragment(), DETAILFRAGMENT_TAG)
                        .commit();
            }
        } else {
            mTwoPane = false;
            Log.i(TAG, "mTwoPane value=" + mTwoPane);

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

请帮我弄清楚我哪里出错了。

【问题讨论】:

  • 您的模拟器似乎没有加载所需的资源。要么,要么您的资源不在您期望的位置。您可能希望在Configuration 对象上记录smallestScreenWidthDp 的值(例如,Activity 上的getResources().getConfiguration())以查看它报告的内容。一个真正的 Nexus 10 应该使用 res/layout-sw600dp/ 资源,假设该资源没有更好的匹配。
  • 在将 mTwoPane 变量的值更改为 true 之前打印它 Log.i(TAG, "mTwoPane value=" + mTwoPane); mTwoPane = true;
  • 我发现我使用的设备(Nexus 10 模拟器)的 sw 为 400dp。我认为 Nexus 10 的 sw 为 800 dp 会以这种方式使用。

标签: android android-layout android-tablet-layout


【解决方案1】:

创建没有框架的新虚拟设备。 AVD Manager->创建虚拟设备->平板电脑->nexus 10->下一步->选择sdk->下一步->取消选中“启用设备框架”

如果有帮助,请标记为已回答。

【讨论】:

    【解决方案2】:

    您是否检查过相关 layout.xml 文件中的电影详细布局的 id 也设置为 movie_detail_container ?它应该与 activity_main.xml 文件的 FrameLayout id 中给出的 id 匹配。

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 2020-12-24
      相关资源
      最近更新 更多