2016-03-28 44 views
0

我正在開發一個簡單的應用程序,您可以在其中看到音樂家列表,並且當您點擊音樂家時,會得到關於它的一些細節(流派,頂級點擊和傳記)。 我想實現的功能是:如果您在寬屏幕上查看它,讓音樂家列表在左邊,細節應該在右邊(點擊音樂家後)。如果它是一個窄屏幕,細節應該單獨出現在新屏幕上。應該使用片段和Framelayouts完成功能。Android活動加載錯誤的佈局文件

所以我有activity_main.xml,和我去添加新的資源文件 - >佈局文件和我說activity_main.xml (w600dp)我希望自動加載的面向景觀的智能手機或平板。

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/muzicari_lista" 
     android:layout_weight="1"> 
    </FrameLayout> 

</LinearLayout> 

w600dp/activity_main.xml中:包含列表和細節有一些的TextBlocks相當簡單的片段

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/lista_muzicara"></FrameLayout> 
    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/detalji_muzicar"></FrameLayout> 
</LinearLayout> 

XML的。 這裏是在MainActivity的類的onCreate方法:

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     unosi = new ArrayList<Muzicar>(); //let's presume there's something in this list 


     wideL = false; 
     FragmentManager fm = getFragmentManager(); 
     //fetching FragmentManager 
     FrameLayout ldetalji = (FrameLayout)findViewById(R.id.detalji_muzicar); 
     if(ldetalji!=null){ 
//layout for wide screens 
      wideL=true; 
      FragmentDetalji fd; 
      fd = (FragmentDetalji)fm.findFragmentById(R.id.detalji_muzicar); 
//checking if there is FragmentDetalji already created: 
      if(fd==null) { 
       //if not, we're creating it now 
       fd = new FragmentDetalji(); 
       fm.beginTransaction().replace(R.id.detalji_muzicar, fd).commit(); 
      } 
     } 

     FragmentLista fl = (FragmentLista)fm.findFragmentByTag("Lista"); 
//we're checking if there's already fl created 

     if(fl==null){ 
      //if it hasn't been created: 
      fl = new FragmentLista(); 
      Bundle argumenti = new Bundle(); 
      argumenti.putParcelableArrayList("Alista",unosi); 
      fl.setArguments(argumenti); 
      fm.beginTransaction().replace(R.id.muzicari_lista, fl).commit(); 
     }else{ 
      //case when we change from portrait to landscape, and there was FragmentDetalji already open 

      fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
     } 
    } 

FragmentDetaljiFragmentLista不這樣做,除了利用onCreateView多特(雖然我可以加入他們,如果他們可能是麻煩的來源)。

問題是,無論我用什麼來加載這個應用程序,activity_main都會以默認形式加載,而不是w600dp版本!我究竟做錯了什麼?

+0

請發表您的res文件夾結構截圖。 –

+0

@Shadab Ansari http://prntscr.com/alaq06 activity_pocetni = activity_main。 –

+0

請貼上您的Manifest文件。 –

回答

0

要提供替代佈局資源,您需要在res文件夾中創建一個名爲layout-sw600dp的子目錄,並將您更改的佈局放在那裏,並將EXACT名稱作爲其他小屏幕布局。然後你的活動應該自動查找這個文件夾。因此,如果您的xml文件的標題爲activity_main.xml,它應該在layout-sw600dp文件夾中具有完全相同的名稱。

您還可以通過爲大屏幕創建名爲layout-landlayout-sw600dp-land的子目錄來爲橫向提供備用佈局。

還要注意是否有使用sw600dpw600dp之間的差異 - 詳情參見這個問題:Difference between sw600dp and w600dp?

+0

Android Studio自動爲我完成了這個工作。我只是不明白它爲什麼不被加載。 –

+0

如果您希望在用戶旋轉屏幕時更改佈局,則需要使用'layout-land'。如果您只是想在更大的屏幕上使佈局不同,請使用'sw600dp'而不是'w600dp'。請參閱我在答案中鏈接的問題,以獲得關於這兩者的更多信息。 – NoChinDeluxe

+0

我期待它在任何足夠寬的屏幕上以相同的方式工作,風景智能手機只是一個例子,它可以是平板電腦以及任何其他設備。 –