动态设置控件背景,透明度

LinearLayout ly = new LinearLayout(getApplicationContext());
ly.setOrientation(LinearLayout.HORIZONTAL);
ly.setGravity(Gravity.CENTER);
ly.setBackground(getResources().getDrawable(R.drawable.location_info_bg));//设置背景色
Drawable a = ly.getBackground();
a.setAlpha(100);//设置透明度
ly.setBackgroundDrawable(a);
ly.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 100));//设置控件宽高
TextView infoView = new TextView(getApplicationContext());
infoButton = new Button(getApplicationContext());
infoButton.setLayoutParams(new ViewGroup.LayoutParams(100, 60));
infoButton.setGravity(Gravity.CENTER);//居中
infoButton.setBackgroundColor(getResources().getColor(R.color.orange_light));//背景颜色
infoButton.setTextSize(10);

View v = new View(getApplicationContext());
v.setLayoutParams(new LinearLayout.LayoutParams(40, 0));
infoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
infoView.setText(mCompany.getName());
infoView.setTextColor(getResources().getColor(R.color.mine_unpressed));
//添加到父控件
ly.addView(infoView);
ly.addView(v);
ly.addView(infoButton);

猜你喜欢

转载自blog.csdn.net/qq_31079677/article/details/51160252
今日推荐