【发布时间】:2016-05-14 14:14:04
【问题描述】:
我正在尝试为我尝试在模拟器 (Nexus 10) 上测试应用程序的 android 平板电脑显示两个窗格。我为此创建了一个activity_man.xml(在文件夹layout-sw600dp 中)。它包含一个专属于它的FrameLayout 和android: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