下面的类永远不会调用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();
}
}
你可以尝试300更高的密度吗? – Snicolas
findViewById()将始终返回null,因为您之前未显示任何内容。 –
这个意见id应该只与一个大型设备一起出现,你能否详细说明一下? –