我有一个包含背景图像的活动,但是当方向变为横向时,我必须更改图像。为此,我添加了 onConfigurationchange()。但它不起作用。图像没有改变。请帮助我
SamActivity.java
package com.example.samworkshop;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.widget.Button;
import android.widget.RelativeLayout;
public class SamActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sam);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sam, menu);
return true;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
RelativeLayout layout =(RelativeLayout)findViewById(R.id.sam);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
layout.setBackgroundResource(R.drawable.sam_back_land);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
layout.setBackgroundResource(R.drawable.sam_back);
}
}
}
activity_sam.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/sam_back"
tools:context=".SamActivity" >
<Button
android:id="@+id/button1"
android:layout_width="250dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="39dp"
android:background="@drawable/buttonsel" />
</RelativeLayout>