0

我正在开发一个 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" />

有谁知道如何修理它?

4

1 回答 1

2

为什么你有2 onCreate()种方法?
为什么他们的签名很奇怪,意义不同public final void onCreate(final Bundle savedInstanceState)呢?

请更正您的方法(并从之前的2 个中选择1个)您的方法,如下所示:onCreate()

public void onCreate(final Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    setContentView(R.layout.activity_main);
    backgroundEl = (RelativeLayout) findViewById(R.id.container);

    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);
        }   
    });
}
于 2015-08-31T10:59:41.703 回答