Android / XML:当RecyclerView为空时,将TextView放在XML的中央

时间:2019-05-18 03:31:48

标签: android xml android-layout

当前,我正在构建一个使用RecyclerView的应用程序。当列表中没有数据时,RecyclerView可能会显示“列表为空”。但是,发生的是,文本绝对位于XML的中心,而不是XML的中心。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/haha"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyHistoryList">
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:maxLines="1"
    android:clickable="false"
    android:focusable="false"
    android:longClickable="false"
    android:textStyle="bold"
    android:textSize="18sp"
    android:textColor="#FFFFFF" />

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

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".80"
            android:gravity="start"
            android:text="Suggestion"
            android:textAllCaps="true"


   android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/colorAccent"
            android:textStyle="bold"
            app:fontFamily="@font/anaheim" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".20"
            android:gravity="start"
            android:text="Status"
            android:textAllCaps="true"

   android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/colorAccent"
            android:textStyle="bold"
            app:fontFamily="@font/anaheim" />
    </TableRow>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recylcerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"/>

    <TextView
        android:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/emptyView"
        android:text="List is Empty"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="30sp"/>
    </LinearLayout>
</LinearLayout>

这是我的Java代码。

if(array.length() == 0){
                            //display empty with your code.
                            emptyView.setVisibility(View.VISIBLE);
                            recyclerView.setVisibility(View.GONE);
                        }else {
                            emptyView.setVisibility(View.GONE);
                            recyclerView.setVisibility(View.VISIBLE);
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject userJson = array.getJSONObject(i);

                                userList.add(new User(
                                        userJson.getInt("id"),
                                        userJson.getString("name").replace("null", ""),
                                        userJson.getString("badgeid").replace("null", ""),
                                        userJson.getString("position").replace("null", ""),
                                        userJson.getString("department").replace("null", ""),
                                        userJson.getString("factory").replace("null", ""),
                                        userJson.getString("reviewer").replace("null", ""),
                                        userJson.getString("title").replace("null", ""),
                                        userJson.getString("year").replace("null", ""),
                                        userJson.getString("month").replace("null", ""),
                                        userJson.getString("suggestionwill").replace("null", ""),
                                        userJson.getString("present").replace("null", ""),
                                        userJson.getString("details").replace("null", ""),
                                        userJson.getString("benefit").replace("null", ""),
                                        userJson.getString("photo").replace("null", ""),
                                        userJson.getString("status").replace("null", ""),
                                        userJson.getString("comment").replace("null", "")
                                ));

下面是我当前的XML

https://scontent.fkul8-1.fna.fbcdn.net/v/t1.0-9/60515690_10216139628056609_7099924834024423424_n.jpg?_nc_cat=106&_nc_eui2=AeHvv0bzaCNYPgIufVyqiMZGqXAQPaArqTPCB4Ix5QXrob9n9jb61O1bt03p-heNH0axIeIO8cOXmEyDzgRZJxNGdYtE_KX0CwckEJnIT_7piA&_nc_ht=scontent.fkul8-1.fna&oh=f6ed096592ceefcb151c71acf9393b63&oe=5D6BF3CC

我要如何使文本“列表为空”位于XML的中心?

3 个答案:

答案 0 :(得分:0)

Simple处理布尔值, 如果有信息,则为真,否则为假 如果布尔值为true,则使回收站可见,并隐藏具有属性visible(gone / visible)的textview;如果为false,则相反。 如果您对视图有疑问,则必须声明textview和recycler,根据情况可以看到它们。

答案 1 :(得分:0)

下面是更新的布局文件。请使用它来解决问题。

<LinearLayout 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:id="@+id/haha"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyHistoryList">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:gravity="center"
        android:longClickable="false"
        android:maxLines="1"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold" />

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

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".80"
                android:gravity="start"
                android:text="Suggestion"
                android:textAllCaps="true"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:textColor="@color/colorAccent"
                android:textStyle="bold"
                app:fontFamily="@font/anaheim" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".20"
                android:gravity="start"
                android:text="Status"
                android:textAllCaps="true"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:textColor="@color/colorAccent"
                android:textStyle="bold"
                app:fontFamily="@font/anaheim" />
        </TableRow>
    </LinearLayout>

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recylcerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:visibility="gone" />

        <TextView
            android:id="@+id/emptyView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="List is Empty"
            android:textSize="30sp"
            android:visibility="visible" />
    </RelativeLayout>
</LinearLayout>

现在,您必须像在没有在recyclerView中获得数据时那样处理代码,只是使recyclerview消失,并使包含“列表为空”的可见textView变为可见。

enter image description here

答案 2 :(得分:0)

将LinearLayout重力设置为中心

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/haha"
     android:gravity="center"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyHistoryList">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:textStyle="bold"
        android:textSize="18sp"
        android:textColor="#FFFFFF" />