RecyclerView设计更改从数组获取

时间:2018-09-11 04:23:05

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

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

我正在使用Fragments和recyclerView从数组中填充数据

enter image description here

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

   

上面是Layout的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>

1 个答案:

答案 0 :(得分:0)

如果您想使用RelativeLayoutalignParentEnd实现最左侧和右侧的布局,则可以使用alignParentStart作为父级。这应该是您的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>