2016-05-14 46 views
2

中使用平板電腦的兩種窗格模式我試圖獲得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); 
    } 
} 

請幫我弄清楚我哪裏會出錯。

+0

您的模擬器似乎沒有加載所需的資源。或者,或者您的資源不在您期望的位置。您可能希望在'Configuration'對象上記錄'smallestScreenWidthDp'的值(例如,'Activity'上的'getResources()。getConfiguration()')以查看它報告的內容。假設沒有更好的資源匹配,真正的Nexus 10應該使用'res/layout-sw600dp /'資源。 – CommonsWare

+0

您在打印mTwoPane變量之前將其值更改爲true Log.i(TAG,「mTwoPane value =」+ mTwoPane); mTwoPane = true; –

+0

我發現我使用的設備(Nexus 10的模擬器)的sw爲400dp。我認爲Nexus 10具有800dp的sw會以這種方式使用。 –

回答

0

無框架創建新的虛擬設備。 AVD Manager->創建虛擬設備 - > tablet-> nexus 10 - >下一步 - >選擇sdk - >下一步 - >取消選中「啓用設備框架」

如果有幫助請標記爲已回答。

0

您是否已檢查過相關layout.xml文件中的電影詳細布局的ID是否也設置爲movie_detail_container?它應該與activity_main.xml文件的FrameLayout id中給出的id匹配。