上周这是工作,但在我做了一些修改后它不起作用(我忘了哪个修改)。所以我建立了一个新项目来测试它,到目前为止找不到原因。
以下是演示:
<?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>
Test.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);
}
}
}
现在,当点击“更改”按钮时,仅调用MainActivity的onConfigurationChanged,未调用“测试”。 这是日志:
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}
答案 0 :(得分:1)
找到此修改位置。 这是因为扩展了AppCompatActivity
但为什么呢? 以及如何使AppCompatActivity调用onConfigurationChanged方法正确。