import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.util.DisplayMetrics; import java.util.ResourceBundle; public class TestMain extends ActionBarActivity { private TextView mTextView01; private TextView mTextView02; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_main); mTextView01 = (TextView) findViewById(R.id.myTextView01); mTextView01.setText("屏幕分辨率"); mTextView01.setTextColor(Color.GREEN); //字体颜色 mTextView01.setBackgroundColor(Color.WHITE); //背景色 DisplayMetrics dm = new DisplayMetrics(); //获取屏幕分辨率 getWindowManager().getDefaultDisplay().getMetrics(dm); String str_2 = "分辨率大小为" + dm.widthPixels + "*" + dm.heightPixels; mTextView02 = (TextView) findViewById(R.id.myTextView02); mTextView02.setText(str_2); }
setBackgroundXXX的用处,设置这个View背景。
setBackgroundDrawable 的参数为Drawable对象,
setBackgroundColor 的参数为Color对象,比如说Color.Red为红色,或Color.rgb(255,0,0) 来制定一个红色
setBackgroundResource 的参数为资源ID,比如说R.drawable.icon
setBackground用法如上
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.util.DisplayMetrics;
import java.util.ResourceBundle;
public class TestMain extends ActionBarActivity {
private TextView mTextView01;
private TextView mTextView02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_main);
mTextView01 = (TextView) findViewById(R.id.myTextView01);
mTextView01.setText("屏幕分辨率");
mTextView01.setTextColor(Color.GREEN); //字体颜色
mTextView01.setBackgroundColor(Color.WHITE); //背景色
DisplayMetrics dm = new DisplayMetrics(); //获取屏幕分辨率
getWindowManager().getDefaultDisplay().getMetrics(dm);
String str_2 = "分辨率大小为" + dm.widthPixels + "*" + dm.heightPixels;
mTextView02 = (TextView) findViewById(R.id.myTextView02);
mTextView02.setText(str_2);
}