> 获取 activity XML中的所有view
private void getAllViews(Activity act) {
List<View> list = getAllChildViews(act.getWindow().getDecorView());
}
private List<View> getAllChildViews(View view) {
List<View> allchildren = new ArrayList<View>();
if (view instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) view;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewchild = vp.getChildAt(i);
allchildren.add(viewchild);
//再次 调用本身(递归)
allchildren.addAll(getAllChildViews(viewchild));
}
}
return allchildren;
}
-- 代码在LinearLayout添加View Android
LinearLayout linearLayout = new LinearLayout(AppContext.getInstance());
linearLayout.setBackgroundColor(Color.BLACK);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams rootParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(rootParams);
ImageView imageView = new ImageView(AppContext.getInstance());
imageView.setImageDrawable(CarLifePresentationController.getInstance().getMaskDrawable());
imageView.setScaleType(ImageView.ScaleType.CENTER);
linearLayout.addView(imageView);
TextView tipsTv = new TextView(AppContext.getInstance());
tipsTv.setText("为了确保您的行驶安全,请在车机屏幕上进行操作。");
tipsTv.setTextColor(Color.WHITE);
tipsTv.setTextSize(CarlifeScreenUtil.getInstance().dip2px(7));
tipsTv.setGravity(Gravity.CENTER_HORIZONTAL);
tipsTv.setPadding(0,20,0,0);
linearLayout.addView(tipsTv);
frameLayout.addView(mTextureView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
frameLayout.addView(linearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
RelativeLayout rlayout = new RelativeLayout(AppContext.getInstance());
TextView tipsTv = new TextView(AppContext.getInstance());
tipsTv.setText("为了确保您的行驶安全,请在车机屏幕上进行操作。");
tipsTv.setTextColor(Color.WHITE);
tipsTv.setTextSize(CarlifeScreenUtil.getInstance().dip2px(7));
tipsTv.setGravity(Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams rLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlayout.addView(tipsTv, rLayoutParams);
frameLayout.addView(rlayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
-- 动态设置view背景颜色setBackgroundColor- https://blog.csdn.net/qq_34765552/article/details/76906404
View.setBackgroundColor(Color.rgb(255, 0, 0));
View.setBackgroundColor(Color.parseColor(“#6495ED”));
android代码设置背景setBackgroundColor无效- https://blog.csdn.net/u013351986/article/details/44497491
Android设置View的背景透明- https://blog.csdn.net/u011368551/article/details/80899838
-- 得到ViewGroup的子View
if (view instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) view;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewchild = vp.getChildAt(i);
allchildren.add(viewchild);
//再次 调用本身(递归)
allchildren.addAll(getAllChildViews(viewchild));
}
}
一、setBackgroundXXX的用处,设置这个View背景。
setBackgroundDrawable 的参数为Drawable对象,;
setBackgroundColor 的参数为Color对象,比如说Color.Red为红色,或Color.rgb(255,0,0) 来制定一个红色;
setBackgroundResource 的参数为资源ID,比如说R.drawable.icon;
-- 设置背景颜色,有三种方法:
1.setBackgroundResource:通过颜色资源ID设置背景色。
textView.setBackgroundResource(R.color.background);
2.setBackgroundColor:通过颜色值设置背景色。
setBackgroundColor方法设置背景:
textView.setBackgroundColor(android.graphics.Color.RED);
3.setBackgroundDrawable:通过Drawable对象设置背景色。
setBackgroundDrawable方法设置背景:Resources resources=getBaseContext().getResources();Drawable drawable=resources.getDrawable(R.color.background);
textView.setBackgroundDrawable(drawable);
/**
* 资源文件中, 根据图片名字获得图片的资源ID
* @param imageName
* @return
*/
public int getImageResID(String imageName) {
int resId = mResources.getIdentifier(imageName, "drawable", context.getPackageName());
return resId;
}
-- imageview怎么在代码中设置src ?
1、iv.setImageBitmap(Bitmap bmp);传入参数为Bitmap
2、iv.setImageDrawable(Drawable able);传入参数为 BitmapDrawable;
3、iv.setImageResource(int rid); 传入参数为图片资源ID.