Добавление видов программно в CardView, который находится внутри RelativeLayout - PullRequest
0 голосов
/ 12 февраля 2019

Мне нужно создать CardViews для RelativeLayout.Внутри CardViews есть другие виды, такие как ImageViews и TextViews.Они добавлены, но, похоже, они не соблюдают правила, которые я добавляю в LayoutParams.Это мой код:

CardView infoCardView=new CardView(HotelActivity.this);
        infoCardView.setId(2);
        RelativeLayout.LayoutParams cardViewParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams iconLp=new RelativeLayout.LayoutParams(20,20);
        RelativeLayout.LayoutParams textLP=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //imagen is another ImageView that is being drawn ok in the RelativeLayout. Also, the cardView is drawn below the ImageView, which is ok.
        cardViewParams.addRule(RelativeLayout.BELOW,imagen.getId());
        infoCardView.setLayoutParams(cardViewParams);
        iconLp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        iconLp.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
        iconLp.setMarginStart(20);
        //Icono del cardView
        ImageView icono=new ImageView(HotelActivity.this);
        icono.setId(3);
        //icono.setLayoutParams(iconLp);
        icono.setBackground(getDrawable(android.R.drawable.ic_dialog_info));
        infoCardView.addView(icono,iconLp);
        //TextView título del CardView
        TextView tvInfo=new TextView(HotelActivity.this);
        tvInfo.setId(4);
        tvInfo.setTextSize(17);
        tvInfo.setText(getString(R.string.info));
        tvInfo.setTypeface(tvInfo.getTypeface(), Typeface.BOLD);
        textLP.addRule(RelativeLayout.END_OF,icono.getId());
        textLP.setMarginStart(20);
        tvInfo.setTextColor(getColor(R.color.white));
        //tvInfo is NOT added to the END of icono, instead is overlapping it.
        infoCardView.addView(tvInfo, textLP);
         infoCardView.setRadius(9);
        infoCardView.setBackgroundColor(Color.parseColor(almacen.getEvento().getBgColor()));
        infoCardView.setElevation(9);
        rl.addView(infoCardView,cardViewParams);

Что я делаю не так?

Спасибо.

1 Ответ

0 голосов
/ 12 февраля 2019

решаемая.Проблема заключалась в том, что я устанавливал некоторые правила для CardView, которые для него недоступны, например CENTER_VERTICAL.Итак, я добавил еще один RelativeLayout в качестве сына CardView, добавил виды в этот RelativeLayout, добавил этот RelativeLayout в CardView, и теперь представления отображаются правильно.

...