通过编码的textview边框

时间:2012-08-31 06:26:40

标签: android

我通过java编码创建了TextView,我需要通过编码绘制边框。请参考下面的代码,

TextView txt = new TextView(this);
txt.setText("SEARCH TABLE");
txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
txt.setFadingEdgeLength(10);
txt.setTextColor(Color.WHITE);
txt.setBackgroundColor(Color.rgb(83,82,82));
txt.setPadding(10, 5, 10, 0);
txt.setGravity(Gravity.CENTER);
txt.setTypeface(Typeface.SERIF,Typeface.BOLD);

怎么做?

1 个答案:

答案 0 :(得分:5)

您可以将形状可绘制(矩形)设置为视图的背景。

txt.setBackgroundDrawable(getResources().getDrawable(R.drawable.black));

矩形drawable back.xml(放入res / drawable文件夹):

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >    
<solid android:color="#ffffff" />    
<stroke android:width="1dip" android:color="#4fa5d5"/>    
</shape>