如何在底部对齐RecyclerView中textview的文本

发布于 2025-01-16 04:08:48 字数 3588 浏览 3 评论 0原文

我正在创建一个文本大小 UI 列表,该列表将在 RecyclerView 中呈现相同文本的不同大小。

要求是,在 RecyclerView 中渲染的文本项应从视图底部渲染,但从顶部渲染。这是我的代码:

RecyclerView声明:

<RelativeLayout
        android:id="@+id/sizes_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:elevation="30dp"
        android:paddingVertical="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <RelativeLayout
            android:id="@+id/size_options"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/subtitles"
            android:layout_gravity="center_horizontal"
            android:layout_marginHorizontal="20dp"
            android:layout_marginTop="16dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/size_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="16dp"
                android:gravity="center_vertical"
                android:minWidth="50dp"
                android:text="@string/size"
                android:textColor="@color/white"
                android:textStyle="bold"
                tools:textColor="@color/black" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/text_track_size_list_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/size_textview"
                android:orientation="horizontal"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
        </RelativeLayout>
    </RelativeLayout>

查看项目声明:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/focus_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="10dp">

    <TextView
        android:id="@+id/row_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:paddingHorizontal="10dp"
        android:paddingVertical="7dp"
        android:textAllCaps="false"
        android:textColor="@drawable/text_size_color"
        tools:text="Aa" />
</FrameLayout>

绑定视图持有者方法:

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val textSizeOption: TextSizeOption = extSizeOptions[position]
        holder.focusView.tag = textSizeOption

        holder.rowItem.textSize = textSizeOption.fontSize
        if(mSelectedFormat != null && mSelectedFormat == textSizeOption.id) {
            holder.focusView.isSelected = true
        } else {
            holder.focusView.isSelected = (position == 0 && mSelectedFormat == null)
        }

    }

我尝试了多种方法,例如将项目视图包装在相对布局中并给出alignParentBottom等,但效果不佳。

这是它现在的样子的屏幕截图: 大小文本顶部对齐

即使大小标签也没有像代码中给出的那样居中对齐。不确定我是否遗漏了什么,但任何帮助将不胜感激

I am creating a list of text sizes UI that will have the different sizes of the same text rendering in the RecyclerView.

The requirement is, the text item that is rendering in the RecyclerView shall render from the bottom of the view, but its rendering from the top. Here is my code:

RecyclerView declaration:

<RelativeLayout
        android:id="@+id/sizes_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:elevation="30dp"
        android:paddingVertical="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <RelativeLayout
            android:id="@+id/size_options"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/subtitles"
            android:layout_gravity="center_horizontal"
            android:layout_marginHorizontal="20dp"
            android:layout_marginTop="16dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/size_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="16dp"
                android:gravity="center_vertical"
                android:minWidth="50dp"
                android:text="@string/size"
                android:textColor="@color/white"
                android:textStyle="bold"
                tools:textColor="@color/black" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/text_track_size_list_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/size_textview"
                android:orientation="horizontal"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
        </RelativeLayout>
    </RelativeLayout>

View Item declaration:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/focus_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="10dp">

    <TextView
        android:id="@+id/row_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:paddingHorizontal="10dp"
        android:paddingVertical="7dp"
        android:textAllCaps="false"
        android:textColor="@drawable/text_size_color"
        tools:text="Aa" />
</FrameLayout>

Bind View Holder method:

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val textSizeOption: TextSizeOption = extSizeOptions[position]
        holder.focusView.tag = textSizeOption

        holder.rowItem.textSize = textSizeOption.fontSize
        if(mSelectedFormat != null && mSelectedFormat == textSizeOption.id) {
            holder.focusView.isSelected = true
        } else {
            holder.focusView.isSelected = (position == 0 && mSelectedFormat == null)
        }

    }

I have tried multiple things like wrapping the item view in the Relative Layout and giving alignParentBottom, etc but that didn't work as well.

Here is the screenshot of how it's looking right now:
Sizes text top aligning

Even the Size label is not centre aligning as it's given in the code. Not sure if I am missing something, but any help will be super appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清浅ˋ旧时光 2025-01-23 04:08:48

像这样修改您的视图项目将解决您的问题

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/focus_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginHorizontal="10dp"
    android:orientation="horizontal">

    <TextView
        android:layout_gravity="center_vertical"
        android:id="@+id/row_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@drawable/text_size_color"
        android:text="Aa" />
</LinearLayout>

Modifying your view item like this will solve your problem

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/focus_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginHorizontal="10dp"
    android:orientation="horizontal">

    <TextView
        android:layout_gravity="center_vertical"
        android:id="@+id/row_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@drawable/text_size_color"
        android:text="Aa" />
</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文