使用ConstraintLayout时如何一起查看许多以编程方式添加的视图?

时间:2019-06-15 00:00:16

标签: android

I'm trying to attempt a UI like this我正在尝试在Android用户界面中使用自定义形状。自定义形状是可绘制的,可在确定屏幕尺寸后重新调整大小,然后在其顶部输出以编程方式生成的文本,但不会显示textview。

我尝试添加textview的方式与添加imageview的方式相同,但是文本不显示。

        setContentView(R.layout.activity_main);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        //Get dimensions based on screen size
        int deviceWidth = metrics.widthPixels;
        double bg_height_double = deviceWidth / 1.714;
        int bg_height = (int) bg_height_double;
        int deviceHeight = metrics.heightPixels;

        //Get Root Layout
        ConstraintLayout dash_layout = 
        getWindow().getDecorView().findViewById(R.id.root_layout);
        ConstraintSet dash_set = new ConstraintSet();

        //Get Custom Background Drawable
        Drawable dash_bg = 
        getResources().getDrawable(R.drawable.dashboard_bg_custom);
        ImageView dash_bg_holder = new ImageView(this);

        //Add Custom BG drawable to root layout
        dash_bg_holder.setId(View.generateViewId());

dash_bg_holder.setImageBitmap(bitmapToDrawable(dash_bg,deviceWidth,bg_height));
        dash_layout.addView(dash_bg_holder);


        //Create TextView1
       TextView tView_Date  = new TextView(this);
        tView_Date.setId(View.generateViewId());

        tView_Date.setText(R.string.date);
        tView_Date.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));
        tView_Date.setTextSize(R.dimen.sub1);
        tView_Date.setTextColor(Color.BLACK);
        Typeface openSansBold = ResourcesCompat.getFont(this, R.font.open_sans_bold);
        tView_Date.setTypeface(openSansBold);
        dash_layout.addView(tView_Date);

        dash_set.clone(dash_layout);
        dash_set.connect(dash_bg_holder.getId(),ConstraintSet.TOP,dash_layout.getId(),ConstraintSet.TOP,0);
        dash_set.connect(tView_Date.getId(),ConstraintSet.TOP,dash_layout.getId(),ConstraintSet.TOP,60);
        dash_set.applyTo(dash_layout);

我希望同时看到textview和imageview,但只显示imageView。

0 个答案:

没有答案