2013-01-12 40 views
0

下面的類永遠不會調用layout-large文件夾中的佈局。這是下面的maintabletlayout.xml文件。我使用的分辨率爲800x1280,密度爲160的avd。下面的findViewById()總是返回null。有任何想法嗎?系統必須具有什麼規格才能訪問佈局大文件夾?爲什麼不能訪問我的佈局大文件夾?

public class MainActivity extends Activity 
{ 
boolean mTwoPane; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    ArrayList<String> subjects=new ArrayList<String>(); 
    subjects.add("Math"); 
    subjects.add("Science"); 
    subjects.add("Art"); 
    if(findViewById(R.id.top_tablet_container)!=null){ 
     mTwoPane=true; 
    }else{ 
     mTwoPane=false; 
    } 

    if(mTwoPane){ 
     setContentView(R.layout.maintabletlayout); 
     SubjectListFragment top=new SubjectListFragment(subjects,mTwoPane); 
     getFragmentManager().beginTransaction().add(R.id.list_container, top).commit(); 
    }else{ 

     setContentView(R.layout.mainphonelayout); 
     SubjectListFragment top=new SubjectListFragment(subjects,mTwoPane); 
     getFragmentManager().beginTransaction().add(R.id.top_phone_container, top).commit(); 

    } 
} 
} 

mainphonelayout.xml

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/top_phone_container" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

</LinearLayout> 

maintabletlayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/top_tablet_container" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    > 
<RelativeLayout 
    android:id="@+id/list_container" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="2" 
    android:layout_toLeftOf="@+id/content_container" 
    > 


</RelativeLayout> 

<RelativeLayout 
    android:id="@id/content_container" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:layout_toRightOf="@id/content_container" 
    > 

</RelativeLayout> 
</LinearLayout> 

在清單我也有這個

<supports-screens 
    android:xlargeScreens="true" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:anyDensity="true" /> 

@nOiAd有正確的答案。我不得不同時給這兩個佈局命名,然後使用該名稱設置ContentView()。通過這種方式,您可以爲適當大小的屏幕設置內容視圖,然後findViewById()在大屏幕布局中嘗試查找ID時能夠返回非空值。

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     ArrayList<String> subjects=new ArrayList<String>(); 
     subjects.add("Math"); 
     subjects.add("Science"); 
     subjects.add("Art"); 
     if(findViewById(R.id.top_tablet_container)!=null){ 
      mTwoPane=true; 
     }else{ 
      mTwoPane=false; 
     } 

     if(mTwoPane){ 

      SubjectListFragment top=new SubjectListFragment(subjects,mTwoPane); 
      getFragmentManager().beginTransaction().add(R.id.list_container, top).commit(); 
     }else{ 


      SubjectListFragment top=new SubjectListFragment(subjects,mTwoPane); 
      getFragmentManager().beginTransaction().add(R.id.top_phone_container, top).commit(); 

     } 
    } 
+0

你可以嘗試300更高的密度嗎? – Snicolas

+0

findViewById()將始終返回null,因爲您之前未顯示任何內容。 –

+0

這個意見id應該只與一個大型設備一起出現,你能否詳細說明一下? –

回答

0

@nOiAd有正確的答案,我必須給兩個佈局同名,然後setContentView()的那個名稱,這樣你設置適當的大小屏幕的內容視圖然後findViewById()在試圖在大屏幕布局中查找ID時能夠返回非空值。

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     ArrayList<String> subjects=new ArrayList<String>(); 
     subjects.add("Math"); 
     subjects.add("Science"); 
     subjects.add("Art"); 
     if(findViewById(R.id.top_tablet_container)!=null){ 
      mTwoPane=true; 
     }else{ 
      mTwoPane=false; 
     } 

     if(mTwoPane){ 

      SubjectListFragment top=new SubjectListFragment(subjects,mTwoPane); 
      getFragmentManager().beginTransaction().add(R.id.list_container, top).commit(); 
     }else{ 


      SubjectListFragment top=new SubjectListFragment(subjects,mTwoPane); 
      getFragmentManager().beginTransaction().add(R.id.top_phone_container, top).commit(); 

     } 
    } 
0

由於Android 3.2,你應該使用layout-swXXXdp符號,其中XXX是數。在你的情況應該是720,一個10英寸的屏幕

+0

爲什麼720指定一個10英寸的屏幕?我正在使用7英寸AVD,並將我的文件夾更改爲您建議的名稱,我沒有運氣。我也試過佈局sw800dp和佈局sw600dp沒有運氣。 –

+0

它在android文檔中。 sw600dp是7英寸。你對minSdkVersion和targetSkdVersion有什麼價值? – Blackbelt

+0

14兩個在清單 –

0

確保您正在運行的模擬器確實已配置爲「大」。分辨率的範圍不會自動適用於大小「桶」(正常/大/ ...) Emulators

+0

我使用克隆nexus 7 avd沒有運氣 –

相關問題