不同的屏幕方向

4
我需要在Android布局方面以外得到一些帮助。比如说:
实际上,我使用了daily.xml这个视图。这个视图包含一个flipper,并且通过编程方式填充了5个ListView,以便用户可以进行翻转。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background">

    <de.oszimtcc.timetableview.TimetableFlipperView
        android:id="@+id/flipper"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_margin="20dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent" >
    </de.oszimtcc.timetableview.TimetableFlipperView>
</LinearLayout>

在TimetableScreen.java中的Flipper

    activity.setContentView(R.layout.daily);
    Log.i(TimetableApplication.LOG_TAG,"Start create Timetable-Aplication");
    activity.setContentView(R.layout.daily);   

    activity.inflater = LayoutInflater.from(this.activity);     
    flipper = (TimetableFlipperView) activity.findViewById(R.id.flipper); 
    dayListView = (ListView) activity.findViewById(R.id.dayListView);
    flipper.AddContent(**Content to show **);

现在,我想添加一个横屏模式。在这种模式下,我不需要翻转器,因为有足够的空间在水平线性布局中显示所有的ListView。所以我创建了一个layout-land资源文件夹并添加了另一个daily.xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/background">

<de.oszimtcc.timetableview.BlockListView
    android:id="@+id/dayListViewMonday"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:padding="5dp"
    android:listSelector="@drawable/day_selector"
    android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent">
</de.oszimtcc.timetableview.BlockListView>

...the same ListView again for 4 times, with different android:id...

</LinearLayout>

但是我该如何在我的TimetableScreen.java类中处理它?因为没有翻转器,我不能使用默认构造函数来初始化类。我应该在每次调用onCreate时调用不同的方法,还是有更好的方法来处理它呢?

非常感谢您,Kooki!

1个回答

3
你可以使用 getResources().getConfiguration().orientation 来查找屏幕方向。如果是横向模式,请不要初始化 flipper 变量。 我在你的代码中添加了一行,请检查 if 控制语句
 activity.setContentView(R.layout.daily);
Log.i(TimetableApplication.LOG_TAG,"Start create Timetable-Aplication");
activity.setContentView(R.layout.daily);   

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_POTRAIT) {   //Check this statement
activity.inflater = LayoutInflater.from(this.activity);     
flipper = (TimetableFlipperView) activity.findViewById(R.id.flipper); 
dayListView = (ListView) activity.findViewById(R.id.dayListView);
flipper.AddContent(**Content to show **);
} else {
//Code for Landscape mode
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接