为纵向和横向指定不同的布局?

时间:2012-03-22 10:15:45

标签: android landscape screen-orientation portrait

我为一个活动指定了两个单独的布局xml文件,一个用于res / layout文件夹中的Portrait,另一个用于res / layout-land文件夹中的Landscape。我的实际问题是,当我以纵向模式启动应用程序时,它需要ui从布局和即时我旋转我的手机它不需要从布局 - 土地文件夹中的xml它需要从布局文件夹ui,如果我在横向模式下启动我的应用程序它从layout-land文件夹中获取xml并立即将我旋转到肖像模式它从布局中获取xml-land ui不会更改为portrait ui。

任何伙伴都可以告诉我,问题是有什么设置需要我帮助我解决这个问题。

提前致谢。

    this is my landscape ui xml

        <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/widget57"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_gravity="center|center_vertical"
        android:layout_marginTop="30dp"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp" >

            <TextView
                android:id="@+id/txtName"
                android:layout_width="90dp"
                android:text="@string/UserName" />

            <EditText
                android:id="@+id/edName"
                android:imeOptions="actionNext|actionDone"
                android:paddingRight="10dp"
                android:singleLine="true"
                android:width="240dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/txtPass"
                android:layout_width="90dp"
                android:text="@string/PassWord" />

            <EditText
                android:id="@+id/edPass"
                android:imeOptions="actionGo"
                android:paddingRight="10dp"
                android:password="true"
                android:singleLine="true"
                android:width="240dp" />
        </TableRow>

         <LinearLayout
            android:id="@+id/widget57"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btnLogin"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="@string/btn_GO"
                android:textSize="19sp" >
            </Button>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="30dp"
                android:text="CoinOp Solutions"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="Version 0.1" />
        </LinearLayout>


    </TableLayout>

    this is my Portrait ui

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

        <TextView
            android:id="@+id/txt_Required"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text=""
            android:textColor="#ffff0000" >
        </TextView>

        <TextView
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="@string/UserName" >
        </TextView>

        <EditText
            android:id="@+id/edName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionNext|actionDone"
            android:singleLine="true"
            android:textSize="18sp" >
        </EditText>

        <TextView
            android:id="@+id/txtPass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/PassWord" >
        </TextView>

        <EditText
            android:id="@+id/edPass"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionGo"
            android:maxLines="1"
            android:password="true"
            android:singleLine="true"
            android:textSize="18sp" >
        </EditText>

        <LinearLayout
            android:id="@+id/widget57"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btnLogin"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="@string/btn_GO"
                android:textSize="19sp" >
            </Button>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="30dp"
                android:text="CoinOp Solutions"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="Version 0.1" />
        </LinearLayout>

    </LinearLayout>

我的清单文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package=""
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:debuggable="true"
        android:icon="@drawable/coinop"
        android:label="@string/app_name"
        android:theme="@style/CustomTheme" >
        <activity
            android:name=".LoginActivity"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateVisible|adjustPan" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ShowDbList"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" >
        </activity>
        <activity
            android:name=".Menu"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan" >
        </activity>

3 个答案:

答案 0 :(得分:7)

您的清单中有android:configChanges="orientation"。根据{{​​3}} configChanges包含您自己手动处理的操作列表。

如果您移除android:configChanges="orientation",则在旋转手机时将更改布局。否则你应该自己处理它。

答案 1 :(得分:2)

不确定这是否有帮助,但你可以尝试(如果你还没有):

  1. 在使用布局的活动中,再次覆盖onConfigurationChanged和setContentView。
  2. 删除android:configChanges =&#34; orientation&#34;对于使用布局的活动

答案 2 :(得分:1)

你应该尝试添加:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}

} 并通过Activity中的setContentView(..)设置布局。 希望这会有所帮助。 查看更多信息: http://developer.android.com/guide/topics/resources/runtime-changes.html

相关问题