上周还可以,但是我做了一些修改后就不行了(我忘了是哪个修改了)。于是我新建了一个项目来测试一下,目前还没有找到原因。
这是演示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private boolean isChange = false;
private Button mChangeBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChangeBtn = (Button) findViewById(R.id.change);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d("MainActivity", newConfig.toString());
int orientation = getResources().getConfiguration().orientation;
if (Configuration.ORIENTATION_LANDSCAPE == orientation) {
mChangeBtn.setBackgroundColor(Color.RED);
} else {
mChangeBtn.setBackgroundColor(Color.BLUE);
}
}
public void screenChange(View view) {
Log.d("MainActivity", "screenChange");
if (!isChange) {
isChange = true;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
isChange = false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.test.MainActivity"
android:orientation="vertical" >
<Button
android:id="@+id/change"
android:text="Change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="screenChange"/>
<com.test.Test
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.test.Test>
</LinearLayout>
测试.java
public class Test extends LinearLayout {
public Test(Context context) {
super(context);
}
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
}
@TargetApi(VERSION_CODES.HONEYCOMB)
public Test(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(VERSION_CODES.LOLLIPOP)
public Test(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d("Test", newConfig.toString());
int orientation = getResources().getConfiguration().orientation;
if (Configuration.ORIENTATION_LANDSCAPE == orientation) {
setBackgroundColor(Color.RED);
} else {
setBackgroundColor(Color.BLUE);
}
}
}
现在,当点击“Change”按钮时,只调用了MainActivity的onConfigurationChanged,而没有调用Test。 这是日志:
D/MainActivity: screenChange
--------- beginning of system
D/MainActivity: {1.0 ?mcc?mnc zh_CN ldltr sw360dp w640dp h335dp 320dpi nrml long land finger -keyb/v/h -nav/h s.12 themeChanged=0 themeChangedFlags=0}
D/MainActivity: screenChange
D/MainActivity: {1.0 ?mcc?mnc zh_CN ldltr sw360dp w360dp h615dp 320dpi nrml long port finger -keyb/v/h -nav/h s.13 themeChanged=0 themeChangedFlags=0}
最佳答案
找到了这个的修改位置。 这是因为扩展了AppCompatActivity
但是为什么呢? 以及如何让AppCompatActivity正确调用onConfigurationChanged方法。
关于java - AppCompatActivity导致CustomView onConfigurationChanged方法未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35882266/