1

我发现类似的主题已经在这里解决了多次,但我无法找到我的问题的解决方案。

我正在使用带有自定义 CursorAdapter 的 ListView,它从数据库中获取数据。这些行是由基于自定义相对布局的 XML 文件创建的,该布局具有图像作为背景,并且应该保持图像的纵横比。相对布局的宽度设置为 match_parent,然后根据图像的纵横比计算高度。

在相对布局中有 5 个项目(实际上有 6 个,第 6 个是一个空视图,只是将行分成两半):1 个 ImageView 和 4 个自定义文本视图,它们被修改为根据文本自动调整大小关于TextView的高度(文字的大小设置为百分比,这里参数heightPercentage为0.5,所以文字的大小应该是TextView高度的50%)。

一切似乎都正常,但有时一些项目没有正确呈现(文本大小似乎是正确的,但 TextView 的宽度太短。向下滚动几个项目然后返回后一切都很好。

这里是照片:

正确一张(滚动后):
正确的

错误一张(滚动前)
不正确

编辑: 我尝试使用互联网上的一些自动调整大小的文本视图,其行为或多或少相似;有时视图在滚动出现之前不会正确呈现,或者它最初是正确呈现的,但是在滚动之后它被搞砸了......在我看来,我在 Listview 行中使用的自定义视图有一些特定的规则不明白:-(我尝试将invalidateViews()、notifyDataSetChanged()等方法放到不同的代码位置,有时甚至像runInIOThread()一样,但没有成功,行为永远不会改变......



下面是名为RelativeLayoutKeepRatio的自定义相对布局、名为WidthResizeTextView的自定义TextView和名为MyCWGCursorAdapter的自定义CursorAdapter的代码。

public class RelativeLayoutKeepRatio extends RelativeLayout {
private float aspectRatio = 0; 
private ViewGroup.LayoutParams mLayoutParams = null;

public RelativeLayoutKeepRatio(Context context) {
    super(context);

}

public RelativeLayoutKeepRatio(Context context, AttributeSet attrs) {
    super(context, attrs);
    aspectRatio = getAspectRatio(context, attrs);
}

public RelativeLayoutKeepRatio(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
    aspectRatio = getAspectRatio(context, attrs);
}

private float getAspectRatio(Context context, AttributeSet attrs)
{
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LayoutKeepRatio);
    float aspectRatio = a.getFloat(R.styleable.LayoutKeepRatio_aspectRatio, 0);
    if (aspectRatio == 0)
    {
        Drawable bg = getBackground();
        if (bg != null)
        {
            int mBgWidth = bg.getIntrinsicWidth();
            int mBgHeight = bg.getIntrinsicHeight();
            aspectRatio = (float)mBgWidth / (float)mBgHeight;
        }
    }
    return aspectRatio;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mLayoutParams == null) {
        mLayoutParams = getLayoutParams();
    }

    int width = 0;
    int height = 0;

    //the width is known and we want to calculate the height
    if ((mLayoutParams.width == ViewGroup.LayoutParams.MATCH_PARENT ||
         mLayoutParams.width == 0
        ) &&
        mLayoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT)
    {
            width = MeasureSpec.getSize(widthMeasureSpec);
            height = calculateHeight(width, aspectRatio);           
    //the height is known and we want to calculate the width
    } else if ((mLayoutParams.height == ViewGroup.LayoutParams.MATCH_PARENT ||
            mLayoutParams.height == 0
            ) &&
            mLayoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT)
    {
        height = MeasureSpec.getSize(heightMeasureSpec);
        width = calculateWidth(width, aspectRatio);
    }

    else //the width and height are known, we do not need to calculate anything
    {
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }

    int mode = MeasureSpec.EXACTLY;
    super.onMeasure(MeasureSpec.makeMeasureSpec(width, mode),
            MeasureSpec.makeMeasureSpec(height, mode));
}

private int calculateWidth(int height, float aspectRatio)
{
    return (int)((float) height * aspectRatio);
}

private int calculateHeight(int width, float aspectRatio)
{
    return (int)((float) width / aspectRatio);
}
}


public class WidthResizeTextView extends TextView {
private float heightPercentage = 0; 

public WidthResizeTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ResizeTextView);
    heightPercentage = a.getFloat(R.styleable.ResizeTextView_HeightPercentage, 0);
}

public WidthResizeTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ResizeTextView);
    heightPercentage = a.getFloat(R.styleable.ResizeTextView_HeightPercentage, 0);
}

public WidthResizeTextView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public float getHeightPercentage(){
    return heightPercentage;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    String text = getText().toString();

    int height = MeasureSpec.getSize(heightMeasureSpec);
    int mode = MeasureSpec.getMode(heightMeasureSpec);
    if (mode == MeasureSpec.EXACTLY){
        setTextSize(TypedValue.COMPLEX_UNIT_PX, (int)((float)height * heightPercentage));
        int neededWidth = (int)getPaint().measureText(text);
        super.onMeasure(MeasureSpec.makeMeasureSpec(neededWidth, mode),
                MeasureSpec.makeMeasureSpec(height, mode));
    }
    else
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}


public class MyCWGCursorAdapter extends CursorAdapter {
String path = null;
Typeface face = null;
public MyCWGCursorAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
    path = CommonUtils.getAppPath(context);
    face = Typeface.createFromAsset(context.getAssets(), "fonts/CANDARA.TTF");
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout row = (RelativeLayout)inflater.inflate(R.layout.row, parent, false);
    ViewWrapper wrapper = new ViewWrapper(row);
    row.setTag(wrapper);
    return (row);
}

@Override
public void bindView(View row, Context context, Cursor cursor) {
    // TODO Auto-generated method stub
    ViewWrapper wrapper = (ViewWrapper)row.getTag();
    String nick = cursor.getString(1);
    String fileName = cursor.getString(2);
    int count = cursor.getInt(3);

    Bitmap bitmap = CommonUtils.applyCircleMask(BitmapFactory.decodeFile(path + fileName));
    if (bitmap == null)
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.empty_cwg);
    ImageView picture = wrapper.getImageView();
    picture.setImageBitmap(bitmap);

    WidthResizeTextView nickLabel = wrapper.getNickLabel();
    nickLabel.setTypeface(face);

    WidthResizeTextView nickTextView = wrapper.getNickTextView();
    nickTextView.setText(nick);
    nickTextView.setTypeface(face);

    WidthResizeTextView countLabel = wrapper.getCountLabel();
    countLabel.setTypeface(face);

    WidthResizeTextView countTextView = wrapper.getCountTextView();
    countTextView.setText("" + count);
    countTextView.setTypeface(face);

}

class ViewWrapper {
    View base;
    ImageView imageView = null;
    WidthResizeTextView nickLabel = null;
    WidthResizeTextView nickTextView = null;
    WidthResizeTextView countLabel = null;
    WidthResizeTextView countTextView = null;

    ViewWrapper(View base){
        this.base = base;
    }
    ImageView getImageView(){
        if (imageView == null){
            imageView = (ImageView)base.findViewById(R.id.CWGView);
        }
        return imageView;
    }

    WidthResizeTextView getNickLabel(){
        if (nickLabel == null) {
            nickLabel = (WidthResizeTextView)base.findViewById(R.id.nickLabel);
        }
        return nickLabel;
    }

    WidthResizeTextView getNickTextView() {
        if (nickTextView == null) {
            nickTextView = (WidthResizeTextView)base.findViewById(R.id.nickTextView);
        }
        return nickTextView;
    }

    WidthResizeTextView getCountLabel(){
        if (countLabel == null) {
            countLabel = (WidthResizeTextView)base.findViewById(R.id.countLabel);
        }
        return countLabel;
    }

    WidthResizeTextView getCountTextView() {
        if (countTextView == null) {
            countTextView = (WidthResizeTextView)base.findViewById(R.id.countTextView);
        }
        return countTextView;
    }
}

}


<?xml version="1.0" encoding="utf-8"?>
<com.asharp.android.CWGs.RelativeLayoutKeepRatio
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.asharp.android.CWGs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/cwg_mycwg_bg" >

        <ImageView
            android:id="@+id/CWGView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="5dip"
            android:adjustViewBounds="true"
            android:layout_alignParentLeft="true" 
            android:layout_alignParentTop="true"     
            android:src="@drawable/empty_cwg" />

        <com.asharp.android.CWGs.WidthResizeTextView
            android:id="@+id/nickLabel"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_above="@+id/spacer"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dip"
            android:layout_toRightOf="@+id/CWGView"
            android:bufferType="spannable"
            android:gravity="left|center_vertical"
            android:singleLine="true"
            android:text="@string/nick"
            android:ellipsize="none"
            android:textColor="@color/MyCWG_field_names"
            custom:HeightPercentage="0.5" />

        <com.asharp.android.CWGs.WidthResizeTextView
            android:id="@+id/nickTextView"
            android:layout_toRightOf="@+id/countLabel"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/spacer"
            android:layout_marginLeft = "10dip"
            android:gravity="left|center_vertical"
            android:text="0"
            android:singleLine="true"
            android:ellipsize="none"
            android:bufferType="spannable"
            custom:HeightPercentage="0.5"
            android:background="#00FF00"
            android:textColor="@color/MyCWG_field_values"/>
        <View
            android:id="@+id/spacer"
            android:layout_width="match_parent"
            android:layout_toRightOf="@id/CWGView"
            android:layout_height="1px"
            android:layout_centerVertical="true"
            android:visibility="invisible"/>            
        <com.asharp.android.CWGs.WidthResizeTextView
            android:id="@+id/countLabel"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/spacer"
            android:layout_marginLeft="10dip"
            android:gravity="left|center_vertical"
            android:layout_toRightOf="@+id/CWGView"
            android:layout_alignParentBottom="true"
            android:text="@string/count_colon"
            android:ellipsize="none"
            android:singleLine="true"
            android:textColor="@color/MyCWG_field_names"
            android:bufferType="spannable"
            custom:HeightPercentage="0.5" />
        <com.asharp.android.CWGs.WidthResizeTextView
            android:id="@+id/countTextView"
            android:layout_toRightOf="@id/countLabel"
            android:layout_alignParentBottom="true"
            android:layout_below="@id/spacer"
            android:layout_marginLeft = "10dip"
            android:gravity="left|center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:ellipsize="none"
            android:bufferType="spannable"
            custom:HeightPercentage="0.5"
            android:text="0"
            android:textColor="@color/MyCWG_field_values"/>


</com.asharp.android.CWGs.RelativeLayoutKeepRatio>


你们中有人见过这种行为吗?我花了几天时间试图解决这个问题,但我没有成功:-(我相信它与 WidthResizeTextView 有关,因为我花了很长时间才能让它至少像这样工作。谢谢帮助!

4

1 回答 1

0

您可能想使用标准视图重新设计您的 XML,例如

  • aConstraintLayout而不是 aRelativeLayout作为容器
  • Guidelines或百分比高度/宽度来定位您的小部件

查看ConstraintLayout 文档

于 2017-12-13T13:52:05.077 回答