gpt4 book ai didi

java - onConfigurationChanged 无法正常工作

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:07 26 4
gpt4 key购买 nike

所以我要做的就是检查方向并根据它更改图像。问题是图像没有变化,toast 消息也没有起作用。不知道为什么..我在其他地方也有同样的代码。

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>

list :

<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。

最佳答案

onConfigurationChanged() 回调只有在我们指定我们将在 Activity 标签内的 manifest.xml 上手动管理此更改时才会被调用:

android:configChanges="orientation|screenSize|keyboardHidden"

这表示当设备旋转时不会重新创建 Activity ,不推荐此行为,除非您有正当理由这样做,例如 youtube API 推荐此操作以避免重新创建和重新初始化YoutubePlayerFragment.

Caution: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications. 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();
}

关于java - onConfigurationChanged 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com