首页 > 解决方案 > 以编程方式添加的 TextView 不会重置为新行的屏幕开头

问题描述

我正在以编程方式在 RecyclerView 中添加的 ViewHolder 中添加 TextView。我面临的问题是在它到达屏幕末尾并转到新行之后,文本与屏幕开头没有对齐。

        mHolder.mAuthorsContainer.removeAllViews();
        int size = mData.mAuthors.mAuthorsHMap.size(), count = 1;
        Typeface typeface = ResourcesCompat.getFont(mContext, R.font.zillaslab_bold);
        
        for(Map.Entry<String, String> entry : mData.mAuthors.mAuthorsHMap.entrySet()) {
           String key = entry.getKey();
           String value = entry.getValue();

           TextView mAuthor_ = new TextView(mContext);
           mAuthor_.setTextColor(Color.parseColor("#3100A2"));
           mAuthor_.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
           mAuthor_.setTypeface(typeface);
           mAuthor_.setTextSize(TypedValue.COMPLEX_UNIT_SP, CustomPreferences.getInstance(mContext).getmSpArticlePublishSize());
           mAuthor_.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, CustomPreferences.getInstance(mContext).getmSpArticleTextLineSpacing(), mMetrics), 1.0f);

           mAuthor_.setText(value);

           mHolder.mAuthorsContainer.addView(mAuthor_);

    }

mAuthorsContainer 是一个 LinearLayout 而父布局是 ConstraintLayout,仅此而已

尝试 setLines、setMaxLines、setGravity 到左侧,目前没有任何效果。

在此处输入图像描述

提前致谢。

标签: androidtextviewandroid-linearlayoutandroid-constraintlayoutandroid-viewholder

解决方案


推荐阅读