【问题标题】:Align reclyerview childers from bottom从底部对齐reclyerview子级
【发布时间】:2019-11-07 23:17:39
【问题描述】:

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

我试过了,reverseLayoutstackFromEnd

我已经左右搜索了。仍然不知道为什么会发生这种情况。

谁能帮帮我。 任何有用的想法将不胜感激。

【问题讨论】:

  • 将视图持有者设置为相对布局并且将内容与底部对齐并不能解决您的问题?
  • @Medeiros 我试图让孩子的父母对齐底部,但这似乎没有做任何事情

标签: java android xml kotlin layout


【解决方案1】:

默认情况下,布局管理器将项目与顶部对齐,因为它是最稳定的,以防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);
      }
});

【讨论】:

    【解决方案2】:

    我有点想通了。

    因为我有静态高度,所以我在孩子周围创建了另一个包装器,它与底部对齐,并且父级设置为特定高度。

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2012-07-02
      • 2020-07-16
      • 1970-01-01
      • 2017-02-16
      相关资源
      最近更新 更多