从底部对齐relyerview childs

从底部对齐relyerview childs

我正在尝试使用reclyerview制作图表。我遇到的问题是我似乎无法使子项从底部而不是从reclyerview的顶部对齐。

我已经尝试过reverseLayoutstackFromEnd

我已左右搜索。仍然不知道为什么会这样。

任何人都可以帮助我。 任何有益的想法将不胜感激。

nuoyi1024 回答:从底部对齐relyerview childs

默认情况下,布局管理器将项目从顶部开始对齐,因为如果RecyclerView的高度为wrap_content,这是最稳定的,这可能会导致一些布局问题。

如果您的RecyclerView具有固定的大小,则可以尝试扩展用于强制其在底部布局项目的布局管理器:

// inside your activity or fragment where you're initializing RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false) {
      @Override
      public void layoutDecoratedWithMargins(@NonNull View child,int left,int top,int right,int bottom) {
          // calculate available space in recyclerView
          int offset = getHeight() - getPaddingTop() - getPaddingBottom();
          // subtract height of item being laid out
          offset -= bottom - top;
          // lay out item lower than usual
          super.layoutDecoratedWithMargins(child,left,top + offset,right,bottom + offset);
      }
});
,

我有点想通了。

由于我的身高是静态的,因此我在子对象周围创建了另一个包装,并将其包装到底部,并将父对象设置为特定的高度。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="80dp"
        android:layout_height="200dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginRight="8dp"
        >

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">


        <TextView
                android:id="@+id/tv_time"
                style="@style/BoldWhiteText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="7.5"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
本文链接:https://www.f2er.com/3141135.html

大家都在问