2013-01-12 36 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没有运气 –

相关问题