onConfigurationChanged无法正常工作

时间:2018-07-11 17:16:32

标签: java android orientation

所以我要做的就是检查方向并基于此更改图像。问题是图像更改或吐司消息均不起作用。不知道为什么。我在其他地方也有相同的代码。

Java:

public class Test extends AppCompatActivity {

private ArrayList<String> mNames = new ArrayList<>();

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

    getText();

}

private void getText() {

    mNames.add("Menu");
    mNames.add("Definitions");
    mNames.add("Steps");
    mNames.add("Examples");
    mNames.add("Related");
    mNames.add("Videos");

    initRecyclerView();
}

private void initRecyclerView() {

    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(layoutManager);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mNames);
    recyclerView.setAdapter(adapter);

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.activity_main);

        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        ImageView layout = findViewById(R.id.background);
        layout.setBackgroundResource(R.drawable.background_l);

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        setContentView(R.layout.activity_main);

        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        ImageView layout = findViewById(R.id.background);
        layout.setBackgroundResource(R.drawable.background_p);
    }
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
    android:id="@+id/background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY" />

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/recyclerView"
        android:paddingTop="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="20dp">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="15dp"
                android:fontFamily="@font/open_sans_bold"
                android:text="@string/definition"
                android:textColor="@color/backgroundBlue"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView2"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/open_sans"
                android:text="@string/fiveSDef1"
                android:textColor="@android:color/background_dark"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView3"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/open_sans_bold"
                android:text="@string/uses"
                android:textColor="@color/backgroundBlue"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView4"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/open_sans"
                android:text="@string/fiveSDef2"
                android:textColor="@android:color/background_dark"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView5"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="15dp"
                android:fontFamily="@font/open_sans"
                android:text="@string/fiveSDef3"
                android:textColor="@android:color/background_dark"
                android:textSize="14sp" />

        </RelativeLayout>
    </ScrollView>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@color/backgroundBlue"
        android:fontFamily="@font/open_sans_bold"
        android:text="@string/fiveS"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="29sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="0dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/button2"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" />
</RelativeLayout>

清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize|screenLayout">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ToolTables.1"></activity>
    <activity android:name=".ToolTables.2"
        android:windowSoftInputMode="adjustPan">
    </activity>
    <activity android:name=".ToolTables.3"></activity>
    <activity android:name=".ToolTables.4"></activity>
    <activity android:name=".ToolTables.5"></activity>
    <activity
        android:name=".Test"
        android:configChanges="orientation|keyboardHidden|screenSize|screenLayout">

    </activity>

    <meta-data
        android:name="preloaded_fonts"
        android:resource="@array/preloaded_fonts" />
</application>

编辑:我已经添加了其余代码。包括与Java耦合的XML。这不是真正的复杂,主要是文本字段和recyclerView。

1 个答案:

答案 0 :(得分:2)

仅当我们指定将在活动标记内的manifest.xml中手动管理此更改时,才会调用

onConfigurationChanged()回调:

android:configChanges="orientation|screenSize|keyboardHidden"

这表明设备旋转时将不会重新创建活动,除非您有充分的理由,否则不建议这种行为,例如youtube API建议这样做以避免重新创建和重新初始化YoutubePlayerFragment。

  

警告: 自行处理配置更改可能会使使用替代资源的难度大大增加,因为系统不会自动为您应用这些资源。当您必须避免由于配置更改而导致重启时,应将这种技术视为万不得已的方法,建议不要将其用于大多数应用程序。   https://developer.android.com/guide/topics/resources/runtime-changes

如果不是必需的,并且只希望每个方向上都有不同的图像,则可以:

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

    boolean isLandscape = this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    ((ImageView)findViewById(R.id.background)).setImageResource(isLandscape ? R.drawable.background_l : R.drawable.background_p);

    getText();
}