代码中新建TextView

TextView.setGravity(Gravity.CENTER);//居中
TextView.setGravity(Gravity.CENTER_HORIZONTAL);//水平居中
TextView.setGravity(Gravity.CENTER_VERTICAL);//垂直居中
TextView.setGravity(Gravity.LEFT);//靠左
TextView.setGravity(Gravity.RIGHT);//靠右

 TextView textView = new TextView(this);//新建控件
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//设置控件的宽高        
layoutParams.setMargins(5, 5, 5, 5);//设置控件与上下左右的距离
textView.setGravity(Gravity.LEFT);
textView.setBackgroundResource(android.R.color.white);//设置背景色 
textView.setTextColor(getResources().getColor(android.R.color.black));//控件字体颜色
textView.setLayoutParams(layoutParams);//上面设置控件的高宽后就落实
textView.setText(message);//控件内容


你可能感兴趣的:(代码中新建TextView)