setContentview()更改onConfigurationChanged()中的布局,但不在android中设置数据

时间:2017-05-19 11:40:38

标签: android xml orientation landscape portrait

我为纵向和横向模式创建了两个布局,我的场景是我想根据方向更改来更改TextView的文本。这是我的代码: -

的AndroidManifest.xml:

<activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

activity_main.xml(portrait)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.demo.textdemo.MainActivity">
<TextView
    android:id="@+id/textViewHello"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_gravity="center"
    android:gravity="center"
    android:textSize="20dp" />

activity_main.xml(landscape):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.demo.textdemo.MainActivity">

<TextView
    android:id="@+id/textViewHello"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World!"
    android:layout_gravity="center"
    android:gravity="center"
    android:textSize="20dp"
    />

MainActivity:

public class MainActivity extends AppCompatActivity {

TextView textViewHello;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textViewHello = (TextView)findViewById(R.id.textViewHello);
    Configuration newConfig = getResources().getConfiguration();
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        textViewHello.setText("ORIENTATION LANDSCAPE");
    }else {

        textViewHello.setText("ORIENTATION PORTRAIT");
    }

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_main);

        textViewHello.setText("ORIENTATION LANDSCAPE");

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_main);
        textViewHello.setText("ORIENTATION PORTRAIT");
    }
}

}

首次,它根据方向显示TextView中的更改,但是当我更改方向时,它显示“Hello World!” ,在activity_main.xml中定义,我不知道为什么?

5 个答案:

答案 0 :(得分:1)

我找到了解决方案。我需要在方向更改时将XML对象转换为Java对象,因为如果方向更改布局也会更改,那么它将需要将XML转换为Java对象。

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_main);
        textViewHello = (TextView)findViewById(R.id.textViewHello);
        textViewHello.setText("ORIENTATION LANDSCAPE");

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_main);
        textViewHello = (TextView)findViewById(R.id.textViewHello);
        textViewHello.setText("ORIENTATION PORTRAIT");
    }

感谢@Dhanumjay

答案 1 :(得分:0)

试试这个

public class MainActivity extends AppCompatActivity {

TextView textViewHello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textViewHello = (TextView)findViewById(R.id.textViewHello);
textViewHello.setText("ORIENTATION PORTRAIT");

}
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    textViewHello.setText("ORIENTATION LANDSCAPE");

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    textViewHello.setText("ORIENTATION PORTRAIT");
}
}

答案 2 :(得分:0)

我找到了解决方案。 只需从清单文件中删除此行,然后一切正常

机器人:configChanges =&#34;取向|屏幕尺寸| keyboardHidden&#34;

答案 3 :(得分:0)

不要更改清单文件并尝试下面的代码。现在它的工作正常而无需重新创建活动

@Override
 public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.test);
    textViewHello = (TextView) findViewById(R.id.textViewHello);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

        textViewHello.setText("ORIENTATION LANDSCAPE");

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        textViewHello.setText("ORIENTATION PORTRAIT");

    }
}

答案 4 :(得分:0)

在android中,只要你想设置有关配置更改的数据,就必须按如下方式调用两个覆盖方法:

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onRestoreInstanceState(savedInstanceState, persistentState);
}