首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onConfigurationChanged没有正常工作

onConfigurationChanged没有正常工作
EN

Stack Overflow用户
提问于 2018-07-11 17:16:31
回答 1查看 337关注 0票数 2

所以我要做的就是检查方向,并在此基础上改变图像。问题是,无论是图像的改变,还是祝酒词的信息都不起作用。不知道为什么..。我在其他地方也有同样的代码。

爪哇:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>

清单:

代码语言:javascript
复制
<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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-11 18:14:17

如果我们指定将手动管理onConfigurationChanged内部活动标记上的manifest.xml ()回调,则只会调用该回调:

代码语言:javascript
复制
android:configChanges="orientation|screenSize|keyboardHidden"

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

警告:自己处理配置更改的会使使用替代资源变得更加困难,因为系统不会自动为您应用这些资源。当您必须避免由于配置更改而重新启动时,这种技术应该被视为最后的手段,并且不推荐在大多数应用程序中使用。https://developer.android.com/guide/topics/resources/runtime-changes

如果它不是相关的,只有你想要在每个方向上有一个不同的图像,你可以:

代码语言:javascript
复制
@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();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51291050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档