【问题标题】:RecyclerView Design Changing fetch from arrayRecyclerView 设计更改从数组中获取
【发布时间】:2018-09-11 04:23:05
【问题描述】:

如果我从 RecyclerView 中的数组中获取数据,屏幕的设计正在发生变化,但是当数据被定义为静态(在 XML 中)时,设计是正确的,我应该怎么做才能保持设计原样在预览中?

我正在使用 Fragments 和 recyclerView 来扩充数组中的数据

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/rentInvoice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="20dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/paymentsDate"
                    android:textStyle="bold"
                    android:textColor="@color/colorFontHeading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/date"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/paymentsReceiptNumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/reciept_number" />
                <TextView
                    android:id="@+id/paymentsData"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="PaymentData" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="end">


                <TextView
                    android:id="@+id/paymentsAmount"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/rs_000000"

                    />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:src="@drawable/ic_navigate_next_black_24dp" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

以上是布局的 XML 代码
下面是RecyclerView所在的XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.PaymentsFragment"
android:orientation="vertical">

<SearchView

    android:queryHint="@string/search"
    android:layout_margin="15dp"
    android:id="@+id/searchViewPayments"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/search_view_rounded"
    android:layoutDirection="rtl"/>


<LinearLayout
    android:id="@+id/recyclerViewLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView

        android:id="@+id/paymentsRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </android.support.v7.widget.RecyclerView>



</LinearLayout>

【问题讨论】:

  • 分享你的 XML 代码
  • 我认为您将主/父布局设置为 wrapContent 这就是为什么会出现设计问题,请与您分享 XML 以获得更好的解决方案。
  • 我已经添加了 XML 代码 @VirRajpurohit
  • 分享你的RecyclerView所在的xml。
  • @Tepits RecyclerView 和 Layout XML 代码都添加了

标签: android android-layout android-studio android-recyclerview recyclerview-layout


【解决方案1】:

如果要实现最左右布局,可以使用RelativeLayout 作为父级,使用alignParentEndalignParentStart。这应该是您在 LinearLayout 标签内的布局,id 为 rentInvoice

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        ...

    </LinearLayout>

    <LinearLayout
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/paymentsAmount"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rs_000000" />

        ...

    </LinearLayout>

</RelativeLayout>

【讨论】:

  • 工作就像一个魅力,谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多