Есть RecyclerView
, вот как выглядит один элемент(item.xml):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background">
<com.company.testapp10.ProportionalImageView
android:id="@+id/imageView"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:src="@drawable/image"
android:adjustViewBounds="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="20dp"/>
</RelativeLayout>
Изображение большое и сжимается в ширине до 110dp соблюдая пропорции, вот код ProportionalImageView:
public class ProportionalImageView extends ImageView {
public ProportionalImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = (w * d.getIntrinsicHeight()) / d.getIntrinsicWidth();
setMeasuredDimension(w, h);
ViewParent viewParent = getParent();
if(viewParent instanceof RelativeLayout) {
RelativeLayout parentLayout = (RelativeLayout) viewParent;
parentLayout.getLayoutParams().height = h;
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
Если item.xml использовать не в списке(первая view на рисунке) нижний отступ у textView
отображается корректно, но если внутри списка(последующие view на рисунке) то его просто нет. После прокручивания списка отступы появляются. Вот как это выглядит:
Как сделать нормальное отображение отступа у textView?