首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我在android中滚动Recyclerview时,项目视图的高度会变小

当我在android中滚动Recyclerview时,项目视图的高度会变小
EN

Stack Overflow用户
提问于 2020-11-04 16:09:26
回答 1查看 65关注 0票数 0

我想显示名称列表。我正在使用recylerview向that.For显示我已将垂直TextView用作项目view.it works properly.But问题是,当我滚动回收视图时,项目高度会减小

VerticalTextview类

代码语言:javascript
复制
public class VerticalTextView extends androidx.appcompat.widget.AppCompatTextView {
    boolean topDown;

    public boolean getTopDown(){
        return topDown;
    }
    public void setTopDown(boolean key){
        this.topDown=key;
    }

    public VerticalTextView(Context context, AttributeSet attrs){
        super(context, attrs);
        final int gravity = getGravity();

        if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
            setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
            topDown = false;
        }else
            topDown = true;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(),getMeasuredWidth());

    }

    @Override
    protected void onDraw(Canvas canvas) {
        TextPaint tp =getPaint();
        tp.setColor(getCurrentTextColor());
        tp.drawableState=getDrawableState();
        canvas.save();

        if(topDown){
            canvas.translate(getWidth(), 0);
            canvas.rotate(90);
        }else {
            canvas.translate(0, getHeight());
            canvas.rotate(-90);
        }

        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

        getLayout().draw(canvas);
        canvas.restore();



    }
}

这是我的项目布局

代码语言:javascript
复制
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sayaki="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">


  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:visibility="visible"

      android:layout_marginVertical="5dp">


        <VerticalTextView
            android:id="@+id/tv_sub_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sathyan"
            android:gravity="center"
            android:paddingVertical="1dp"
            android:maxLines="1"
            android:textSize="14sp"
            android:layout_marginVertical="10dp"
            android:textColor="@color/black"
            android:textStyle="bold"
        />


  </LinearLayout>

</layout>

但在滚动时,项的高度会减小。

这是滚动前的:

滚动后:

您可以看到,最后3个项目的高度降低了,一些c

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-06 12:28:41

我找到了问题的解决方案,我只是像下面这样修改了我的垂直TextView类

代码语言:javascript
复制
public class NewVerticalView extends TextView {
    private Rect bounds = new Rect();
    private TextPaint textPaint;
    private int color;

    public NewVerticalView(Context context) {
        super(context);
    }

    public NewVerticalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        color = getCurrentTextColor();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        textPaint = getPaint();
        textPaint.getTextBounds((String) getText(), 0, getText().length(), bounds);
        setMeasuredDimension((int) (bounds.height() + textPaint.descent()), bounds.width());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        textPaint.setColor(color);
        canvas.rotate(-90,bounds.width(),0
        );
        canvas.drawText((String) getText(), 0,- bounds.width() + bounds.height(), textPaint);
    }
}

并在xml.Now中添加了180度旋转,我的问题就解决了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64676222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档