我正在开发一个 Android 应用程序,我想通过使用切换按钮(“深色”和“浅色”主题)来更改应用程序的背景颜色。
这是我到目前为止尝试过的代码,但它不会改变颜色,尽管没有给出任何错误。
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private RelativeLayout backgroundEl;
private ToggleButton toggle;
protected void onCreate(RelativeLayout RelativeLayout) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
this.onCreate(backgroundEl = (RelativeLayout) findViewById(R.id.container));
}
protected void onCreate(ToggleButton toggleButton) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
this.onCreate(toggle = (ToggleButton) findViewById(R.id.toggleButton1));
toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (toggle.isChecked())
backgroundEl.setBackgroundColor(Color.BLACK);
else
backgroundEl.setBackgroundColor(Color.WHITE);
}
});
}
}
这是activity_main.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.avatar_test.MainActivity" >
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/gallery1"
android:layout_below="@+id/imageView1"
android:layout_marginBottom="10dp"
android:text="ToggleButton" />
有谁知道如何修理它?