Java 在Android studio中为TextView添加背景色
我正在尝试为Android studio中的两个文本视图添加背景色,一个应该是红色,另一个应该是蓝色。我现在把它设置为随机Java 在Android studio中为TextView添加背景色,java,android,android-studio,Java,Android,Android Studio,我正在尝试为Android studio中的两个文本视图添加背景色,一个应该是红色,另一个应该是蓝色。我现在把它设置为随机 public void buildGuiByCode( Activity activity, int width, int height, int numberOfPieces ) { positions = new int[numberOfPieces]; tvs = new
public void buildGuiByCode( Activity activity, int width, int height,
int numberOfPieces )
{
positions = new int[numberOfPieces];
tvs = new TextView[numberOfPieces];
colors = new int[tvs.length];
params = new LayoutParams[tvs.length];
Random random = new Random( );
labelHeight = height ;
labelwidth = width / numberOfPieces;
for( int i = 0; i < tvs.length; i++ ) {
tvs[i] = new TextView( activity );
tvs[i].setGravity( Gravity.CENTER);
colors[i] = Color.rgb( random.nextInt(255 ),
random.nextInt(255 ), random.nextInt(255 ) );
tvs[i].setBackgroundColor( colors[i] );
params[i] = new LayoutParams( labelwidth, height );
params[i].topMargin = 0;
params[i].leftMargin = labelwidth * i;
addView( tvs[i], params[i] );
}
}
public void buildGuiByCode(活动活动、整数宽度、整数高度、,
整数(件)
{
位置=新整数[件数];
tvs=新文本视图[numberOfPieces];
颜色=新整数[电视长度];
params=新布局params[tvs.length];
Random Random=新的Random();
标签高度=高度;
标签宽度=宽度/件数;
对于(int i=0;i
您可以使用setBackgroundResource
设置背景色。这里是一个片段
TextView textView = new TextView(activity);
textView.setBackgroundResource(R.color.red);
参考此官方文件是背景色还是文本色?背景色可能是@CharlieBatista的副本,仅用于记录接受解决方案,您认为这对您有帮助。您需要单击正确的符号来接受answer@CharlieBatista你可能会喜欢这篇文章