TextView textView = new TextView(MainActivity.this);
//设置LayoutParams 否则会空指针异常 textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); if(i==0){ textView.setBackgroundColor(getResources().getColor(R.color.red)); }else { textView.setBackgroundColor(getResources().getColor(R.color.gray)); } textView.setText((i+1)+""); textView.setGravity(Gravity.CENTER); textView.setWidth(30); textView.setWidth(30);
//在获取LayoutParams之前要设置LayoutParams 否则会空指针异常 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) textView.getLayoutParams(); lp.setMargins(10, 10, 10, 10); textView.setLayoutParams(lp); textView.setTextColor(getResources().getColor(R.color.white));
本文介绍如何在Android应用中创建并自定义TextView组件,包括设置背景颜色、文字颜色、宽度及对其方式等,并展示了如何使用LayoutParams调整TextView的边距。
1万+

被折叠的 条评论
为什么被折叠?



