【问题标题】:ImageView custom height based on TextView heightImageView自定义高度基于TextView高度
【发布时间】:2018-08-16 19:03:16
【问题描述】:

您好,我在根据 TextView 的高度设置 ImageView 的高度时遇到问题,这是我正在使用的代码:

    @Override
public void onBindViewHolder(@NonNull final 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);

    holder.abstractText.post(new Runnable() {
        @Override
        public void run() {

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, holder.abstractText.getHeight() + 300);
            holder.borderImage.setLayoutParams(layoutParams);
        }
    });
}

问题是在我开始滚动 RecyclerView 之前一切正常,当我滚动图像的高度小于 TextView 时,我该如何解决这个问题?

【问题讨论】:

    标签: android android-recyclerview height


    【解决方案1】:

    主要结构必须是这样的:(RelativeLayout的高度=wrap_content,TextView的高度=wrap_content,ImageView的高度=match_parent)

    <RelativeLayout 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content">
    
        <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text"/>
    
        <ImageView
        android:id="@+id/imageview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/any"/>
    
    </RelativeLayout>
    

    如果您使用的是 RecycyclerView,请托盘:

     @Override
       public int getItemViewType(int position) {
      return position;
     }
    

    【讨论】:

    • xml的结构是正确的,但问题总是存在,你有没有“用(View.getTag())回收膨胀视图”的例子,因为我不知道它是什么,谢谢!
    猜你喜欢
    • 2019-01-21
    • 1970-01-01
    • 2014-08-10
    • 2016-10-25
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多