下面的類永遠不會調用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應該只與一個大型設備一起出現,你能否詳細說明一下? –