动态添加子View,遍历子view,src/background设置背景

> 获取 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值