中使用平板電腦的兩種窗格模式我試圖獲得android平板電腦的兩種窗格顯示方式我試圖在模擬器上測試應用程序(Nexus 10)。我爲此創建了一個activity_man.xml
(在文件夾layout-sw600dp
中)。它包含一個FrameLayout
排除它與android:id="@+id/movie_detail_container"
如下所述。無法在Android的
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 /'資源。 – CommonsWare
您在打印mTwoPane變量之前將其值更改爲true Log.i(TAG,「mTwoPane value =」+ mTwoPane); mTwoPane = true; –
我發現我使用的設備(Nexus 10的模擬器)的sw爲400dp。我認爲Nexus 10具有800dp的sw會以這種方式使用。 –