我想寫一個程序,將屏幕的背景顏色更改爲我決定的顏色。 我寫了這樣的事情,但是當它運行它崩潰 是什麼STHE問題,請大家幫忙me.here是XML代碼改變佈局顏色android應用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#FFFFFF"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="White" />
</LinearLayout>
</LinearLayout>
而這裏的.java代碼
package com.example.flashlight;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class FlashLight extends Activity {
Button red,green,blue,white;
LinearLayout view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
red=(Button) findViewById(R.id.button1);
green=(Button) findViewById(R.id.button2);
blue=(Button) findViewById(R.id.button3);
white=(Button) findViewById(R.id.button4);
red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.RED);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_flash_light, menu);
return true;
}
}
我做了你所說的,它只改變了按鈕的線條而不是大廳背景。 – nomad
您需要將'android:id = @ + id/view'添加到基礎佈局,這意味着涵蓋所有背景的所有背景 – thepoosh
非常感謝。您保存了一天:D – nomad