close
1.可在xml檔中修改,紅色那條
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/back"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff000000"
>
</LinearLayout>
2.在Activity程式中修改(先在xml的layout加入id如上圖綠色 )
package com.AndroidBackground;
import android.app.Activity;
import android.graphics.Color;//這行要自己import
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class AndroidBackground extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//R.id.back的back是xml裡的Layout的id
final LinearLayout background = (LinearLayout)findViewById(R.id.back);
Button buttonRed = (Button)findViewById(R.id.red);
Button buttonGreen = (Button)findViewById(R.id.green);
Button buttonBlue = (Button)findViewById(R.id.blue);
buttonRed.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v) {
//下行為設定background.setBackgroundColor(Color.RED);
//或是
background.setBackgroundColor(Color.parseColor("色碼")
)
}
});
buttonGreen.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
background.setBackgroundColor(Color.GREEN);
}
});
buttonBlue.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
background.setBackgroundColor(Color.BLUE);
}
});
}
}
color的話有分import的Color可以用 也可以自訂color.xml檔
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="red">#7f00</drawable>
</resources>
但是自訂又要在程式裡設定的話要加比較多東西
private Drawable myDeawable;
private LinearLayout l;
l=(LinearLayout) findViewById(R.id.layout的id);
Resources resources = getBaseContext().getResources();
myDeawable = resources.getDrawable(R.drawable.red);//R.drawable.red為xml裡設定的顏色
l.setBackgroundDrawable(myDeawable);
文章標籤
全站熱搜