私は 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" />
誰もそれを修正する方法を知っていますか?