根据 TextView 的高度设置 ImageView 的高度
Set the height of a ImageView based on the height of a TextView
我有一个 ImageView,它应该具有 TextView 的高度,这是我正在使用的代码,但它总是 returns 0:
@Override
public void onBindViewHolder(@NonNull viewHolder holder, final int position) {
holder.abstractText.setText(events.get(position).abstractTExt);
Rect bounds = new Rect();
Paint textPaint = holder.abstractText.getPaint();
textPaint.getTextBounds((String) holder.abstractText.getText(),0,holder.abstractText.getText().length(),bounds);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, bounds.height());
holder.borderImage.setLayoutParams(layoutParams);
Log.d("-------------borderHe", String.valueOf((holder.abstractText.getLineHeight() * holder.abstractText.getLineCount())));
}
我该怎么做?提前致谢!
它返回零,因为您的视图尚未创建,因此您需要通过 post 方法使用以下代码。
首先需要计算TextView的高度,然后应用到layout,
holder.abstractText.post(new Runnable() {
@Override
public void run() {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, holder.abstractText.getHeight());
holder.borderImage.setLayoutParams(layoutParams);
}
});
我有一个 ImageView,它应该具有 TextView 的高度,这是我正在使用的代码,但它总是 returns 0:
@Override
public void onBindViewHolder(@NonNull viewHolder holder, final int position) {
holder.abstractText.setText(events.get(position).abstractTExt);
Rect bounds = new Rect();
Paint textPaint = holder.abstractText.getPaint();
textPaint.getTextBounds((String) holder.abstractText.getText(),0,holder.abstractText.getText().length(),bounds);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, bounds.height());
holder.borderImage.setLayoutParams(layoutParams);
Log.d("-------------borderHe", String.valueOf((holder.abstractText.getLineHeight() * holder.abstractText.getLineCount())));
}
我该怎么做?提前致谢!
它返回零,因为您的视图尚未创建,因此您需要通过 post 方法使用以下代码。 首先需要计算TextView的高度,然后应用到layout,
holder.abstractText.post(new Runnable() {
@Override
public void run() {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, holder.abstractText.getHeight());
holder.borderImage.setLayoutParams(layoutParams);
}
});