在Activity中以编程方式添加CompoundView无法正常工作

时间:2019-08-28 08:05:32

标签: android android-layout android-custom-view android-constraintlayout

我已经通过扩展FrameLayout创建了一个复合视图。我在xml文件中测试了这个组件,它的工作正常,但是当我在约束布局中动态添加该组件时,它没有显示,当我在组件的xml中设置特定高度时,在每种情况下它的工作都很好,但是没有在其中显示TextViews ,以下是用于动态创建此组件的代码:

WelcomeNoteView welcomeNoteView = new WelcomeNoteView(this);
        welcomeNoteView.setLayoutParams(new ConstraintLayout.LayoutParams
                (ConstraintLayout.LayoutParams.MATCH_PARENT,
                        ConstraintLayout.LayoutParams.WRAP_CONTENT));
        welcomeNoteView.setTitle(R.string.title_box_detail_welcome);
        welcomeNoteView.setSubTitle(R.string.message_box_detail_welcome);
        welcomeNoteView.setTitleGravity(Gravity.START);
        welcomeNoteView.setSubTitleGravity(Gravity.START);
        welcomeNoteView.setTitleFont(ResourcesCompat.getFont(context, R.font.helvetica));
        welcomeNoteView.setSubTitleFont(ResourcesCompat.getFont(context, R.font.helvetica));
        welcomeNoteView.setTitleTextSize(R.dimen.welcome_card_header_large_text_size);
        welcomeNoteView.setSubTitleTextSize(R.dimen.welcome_card_header_subtitle_text_size);
        welcomeNoteView.setTopBottomTitlePadding(R.dimen.box_detail_welcome_card_title_padding_top, R.dimen.zero);
        welcomeNoteView.setTopBottomSubTitlePadding(R.dimen.box_detail_welcome_card_subtitle_padding_top, R.dimen.box_detail_welcome_card_subtitle_padding_bottom);
        welcomeNoteView.setId(View.generateViewId());
        binding.parentLayout.addView(welcomeNoteView);
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(binding.parentLayout);
        constraintSet.connect(welcomeNoteView.getId(), ConstraintSet.START,
                binding.backIv.getId(), ConstraintSet.START);
        constraintSet.connect(welcomeNoteView.getId(), ConstraintSet.END,
                ConstraintSet.PARENT_ID, ConstraintSet.END);
        constraintSet.connect(welcomeNoteView.getId(), ConstraintSet.TOP,
                binding.tvTitle.getId(), ConstraintSet.BOTTOM);
        constraintSet.connect(binding.swipeRefreshLayout.getId(), ConstraintSet.TOP,
                welcomeNoteView.getId(), ConstraintSet.BOTTOM);
        constraintSet.connect(binding.slEffect.getId(), ConstraintSet.TOP,
                welcomeNoteView.getId(), ConstraintSet.BOTTOM);
        constraintSet.applyTo(binding.parentLayout);

班级:

@BindingMethods({
        @BindingMethod(type = WelcomeNoteView.class, attribute = "title", method = "setTitle")
})
public class WelcomeNoteView extends FrameLayout {
    private LayoutWelcomeNoteViewBinding itemViewBinding;
    public WelcomeNoteView(Context context) {
        super(context);
        initializeView(context, null, 0);
    }
    public WelcomeNoteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeView(context, attrs, 0);
    }
    public WelcomeNoteView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initializeView(context, attrs, defStyleAttr);
    }
    private void initializeView(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WelcomeNoteView,
                defStyleAttr, 0);
        String title = null, subTitle = null;
        int subTitleLines = 1;
        int titleFont = -1;
        int subTitleFont = -1;
        int titleGravity = -1;
        int subTitleGravity = -1;
        float titleTextSize = -1;
        float subTitleTextSize = -1;
        float titlePaddingTop = 0;
        float titlePaddingBottom = 0;
        float subTitlePaddingTop = 0;
        float subTitlePaddingBottom = 0;
        try {
            int n = a.getIndexCount();
            for (int i = 0; i < n; i++) {
                int attr = a.getIndex(i);
                switch (attr) {
                    case R.styleable.WelcomeNoteView_title:
                        title = a.getString(attr);
                        break;
                    case R.styleable.WelcomeNoteView_subTitle:
                        subTitle = a.getString(attr);
                        break;
                    case R.styleable.WelcomeNoteView_subTitleLines:
                        subTitleLines = a.getInt(attr, subTitleLines);
                        break;
                    case R.styleable.WelcomeNoteView_titleFont:
                        titleFont = a.getResourceId(attr, R.font.sfui_text_semibold);
                        break;
                    case R.styleable.WelcomeNoteView_subTitleFont:
                        subTitleFont = a.getResourceId(attr, R.font.sfui_text_semibold);
                        break;
                    case R.styleable.WelcomeNoteView_titleTextSize:
                        titleTextSize = a.getDimension(attr, R.dimen.welcome_card_header_large_text_size);
                        break;
                    case R.styleable.WelcomeNoteView_subTitleTextSize:
                        subTitleTextSize = a.getDimension(attr, R.dimen.welcome_card_header_subtitle_text_size);
                        break;
                    case R.styleable.WelcomeNoteView_titleGravity:
                        titleGravity = a.getInteger(attr, Gravity.CENTER);
                        break;
                    case R.styleable.WelcomeNoteView_subTitleGravity:
                        subTitleGravity = a.getInteger(attr, Gravity.CENTER);
                        break;
                    case R.styleable.WelcomeNoteView_titlePaddingTop:
                        titlePaddingTop = a.getDimension(attr, R.dimen.zero);
                        break;
                    case R.styleable.WelcomeNoteView_subTitlePaddingTop:
                        subTitlePaddingTop = a.getDimension(attr, R.dimen.zero);
                        break;
                    case R.styleable.WelcomeNoteView_titlePaddingBottom:
                        titlePaddingBottom = a.getDimension(attr, R.dimen.zero);
                        break;
                    case R.styleable.WelcomeNoteView_subTitlePaddingBottom:
                        subTitlePaddingBottom = a.getDimension(attr, R.dimen.zero);
                        break;
                    default:
                        AppLogger.d("SignUpTitleView", "Unknown attribute for " + getClass().toString() + ": " + attr);
                        break;
                }
            }
        } finally {
            a.recycle();
        }
        itemViewBinding = LayoutWelcomeNoteViewBinding.inflate(LayoutInflater.from(context),
                this, true);
        if (!TextUtils.isEmpty(title))
            setTitle(title);
        if (!TextUtils.isEmpty(subTitle))
            setSubTitle(subTitle);
        if (subTitleLines > 1) {
            itemViewBinding.tvSubTitle.setLines(subTitleLines);
        }
        if (titleFont > -1)
            setTitleFont(ResourcesCompat.getFont(context, titleFont));
        if (subTitleFont > -1)
            setSubTitleFont(ResourcesCompat.getFont(context, subTitleFont));
        if (titleTextSize > -1)
            setTitleTextSize(titleTextSize);
        if (subTitleTextSize > -1)
            setSubTitleTextSize(subTitleTextSize);
        if (titleGravity > -1)
            setTitleGravity(titleGravity);
        if (subTitleGravity > -1)
            setSubTitleGravity(titleGravity);
        setTopBottomTitlePadding((int) titlePaddingTop, (int) titlePaddingBottom);
        setTopBottomSubTitlePadding((int) subTitlePaddingTop, (int) subTitlePaddingBottom);
    }
    public void setTitleGravity(int titleGravity) {
        itemViewBinding.tvTitle.setGravity(titleGravity);
    }
    public void setSubTitleGravity(int titleGravity) {
        itemViewBinding.tvSubTitle.setGravity(titleGravity);
    }
    public void setTitleTextSize(float titleTextSize) {
        itemViewBinding.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
    }
    public void setSubTitleTextSize(float titleTextSize) {
        itemViewBinding.tvSubTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
    }
    public void setTitleFont(Typeface font) {
        itemViewBinding.tvTitle.setTypeface(font);
    }
    public void setSubTitleFont(Typeface font) {
        itemViewBinding.tvSubTitle.setTypeface(font);
    }
    public void setTopBottomSubTitlePadding(int subTitlePaddingTop, int subTitlePaddingBottom) {
        itemViewBinding.tvSubTitle.setPadding(0, (int) subTitlePaddingTop,
                0, (int) subTitlePaddingBottom);
    }
    public void setTopBottomTitlePadding(int titlePaddingTop, int titlePaddingBottom) {
        itemViewBinding.tvTitle.setPadding(0, (int) titlePaddingTop,
                0, (int) titlePaddingBottom);
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }
    public TextView getTitleTextView() {
        return itemViewBinding.tvTitle;
    }
    public String getTitle() {
        return itemViewBinding.tvTitle.getText().toString();
    }
    public void setTitle(@StringRes int resId) {
        itemViewBinding.tvTitle.setText(resId);
    }
    public void setTitle(String text) {
        itemViewBinding.tvTitle.setText(text);
    }
    public TextView getSubTitleTextView() {
        return itemViewBinding.tvSubTitle;
    }
    public String getSubTitle() {
        return itemViewBinding.tvSubTitle.getText().toString();
    }
    public void setSubTitle(@StringRes int resId) {
        setSubTitle(getContext().getString(resId));
    }
    public void setSubTitle(String text) {
        if (!TextUtils.isEmpty(text)) {
            itemViewBinding.tvSubTitle.setText(text);
        } else
            itemViewBinding.tvSubTitle.setText("");
    }
}

这是组件的xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_welcome_card"
        android:clipToPadding="false"
        android:shadowColor="#4746bbc2"
        android:shadowDy="22.5"
        android:shadowRadius="10.4">
        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/welcome_card_head_side_margins"
            android:layout_marginRight="@dimen/welcome_card_head_side_margins"
            android:fontFamily="@font/sfui_text_semibold"
            android:gravity="center_horizontal"
            android:letterSpacing="-0.01"
            android:textColor="#ffffff"
            android:textSize="@dimen/welcome_card_header_large_text_size"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/title_home_welcome" />
        <TextView
            android:id="@+id/tvSubTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:fontFamily="@font/sfui_text_regular"
            android:letterSpacing="-0.01"
            android:lineSpacingExtra="6.1sp"
            android:textColor="#ffffff"
            android:textSize="@dimen/welcome_card_header_subtitle_text_size"
            app:layout_constraintEnd_toEndOf="@+id/tvTitle"
            app:layout_constraintStart_toStartOf="@+id/tvTitle"
            app:layout_constraintTop_toBottomOf="@+id/tvTitle"
            tools:text="@string/message_home_welcome" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 个答案:

没有答案