编程实现颜色变幻
package com.example.ok4;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.graphics.Color;
public class MainActivity extends Activity {
private LinearLayout linearlayout; //声明LinearLayout
private LinearLayout.LayoutParams layoutparams; //声明LayoutParams
private TextView tv_blue,tv_black,tv_yellow; //声明TextView
private int wc=LinearLayout.LayoutParams.WRAP_CONTENT; //声明一个int型的 wc=LinearLayout.LayoutParams.WRAP_CONTENT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout=new LinearLayout(this); //初始化一个LinearLayout
linearlayout.setOrientation(LinearLayout.VERTICAL); //设置LinearLayout方向是垂直
linearlayout.setBackgroundColor(Color.RED); //设置背景色是红色
setContentView(linearlayout); //显示出来LinearLayout
layoutparams=new LinearLayout.LayoutParams(wc,wc); //初始化一个LayoutParams
addText(); //初始化TextView并设置值
addTextView(); //把TextView添加到LinearLayout中
}
public void addText()
{
tv_blue=new TextView(this);
tv_black=new TextView(this);
tv_yellow=new TextView(this);
tv_blue.setText("blue");
tv_blue.setTextColor(Color.BLUE);
tv_black.setText("black");
tv_black.setTextColor(Color.BLACK);
tv_yellow.setText("yellow");
tv_yellow.setTextColor(Color.YELLOW);
}
public void addTextView()
{
linearlayout.addView(tv_blue,layoutparams);
linearlayout.addView(tv_black,layoutparams);
linearlayout.addView(tv_yellow,layoutparams);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}