【问题标题】:Layout not visible on Android布局在 Android 上不可见
【发布时间】:2015-06-20 17:12:39
【问题描述】:

我有一个SwipeRefreshLayout。如果用户没有流我想显示错误页面,否则RecyclerView

RecyclerView 工作正常,但如果用户没有流式传输,则不会显示错误页面。

这是我的代码

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_stream"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/feed_item_padding_topbottom"
            android:paddingTop="@dimen/feed_item_padding_topbottom"
            android:scrollbars="vertical" />

        <LinearLayout
            android:id="@+id/error_page"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="@dimen/no_found_width"
                android:layout_height="@dimen/no_found_height"
                android:layout_marginBottom="15dp"
                android:src="@drawable/ic_no_found" />

            <TextView
                android:id="@+id/error_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/not_found"
                android:textSize="@dimen/error_page_textsize" />
        </LinearLayout>

    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

【问题讨论】:

  • 你有java代码吗?这种方式很难理解。另外我认为你应该使用match_parent 而不是fill_parent
  • SwipeRefreshLayout 只带一个孩子。将您的错误页面放在它之外的根布局中。还将您的根布局从LinearLayout 更改为FrameLayout

标签: android android-layout swiperefreshlayout


【解决方案1】:

您可以通过调整来控制视图的可见性 可见属性。

有三种可能的状态:

VISIBLE、INVISIBLE 和 GONE(不占用布局中的空间)。

例如。

// no error
errorView.setVisibility(View.GONE); 
recylverView.setVisibility(View.VISIBLE);

// if error
errorView.setVisibility(View.VISIBLE); 
recylverView.setVisibility(View.GONE);

有关控制可见性的更多详细信息,请参阅此链接。 http://developer.android.com/reference/android/view/View.html#setVisibility(int)

您还可以在布局 xml 文件中设置属性。 可能将错误视图设置为“GONE”是有意义的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多