动态改变Layout背景色
转自:http://h3clikejava.blog.163.com/blog/static/1344730292010102315035426/
动态改变Layout背景色(Change Layout Background)
| 关键代码:LinearLayout.setBackgroundColor(Color.parseColor("#FF00FF")); 其他Color功能详见API。 |

package com.h3c.ColorDroid;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class ColorDroid extends Activity {
Button button;
EditText text;
LinearLayout lly;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button1);
text = (EditText)findViewById(R.id.editText1);
lly = (LinearLayout)findViewById(R.id.main1);
button.setOnClickListener(new buttonClick());
}
class buttonClick implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String colorCode = "#"+text.getText().toString();
lly.setBackgroundColor(Color.parseColor(colorCode));
}
}
}
color.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<drawable name="white">#FFFFFF</drawable>
<drawable name="pink">#FFC8FF</drawable>
</resources>
运行图:


浙公网安备 33010602011771号