【问题标题】:Android list view with empty state shrinks root view具有空状态的 Android 列表视图会缩小根视图
【发布时间】:2016-06-09 12:02:32
【问题描述】:

您好,我正在开发 Android 应用程序,我在其中使用带有对话框片段的列表视图。我还尝试为我的列表视图设置空视图。一切正常,只有我的列表视图在显示空视图后缩小。我尝试了以下方式:

    View emptyView = inflater.inflate(R.layout.no_result_found_layout, null, false);
    ViewGroup viewGroup= ( ViewGroup)countryListView.getParent();
    viewGroup.addView(emptyView, countryListView.getLayoutParams());
    countryListView.setEmptyView(emptyView);

我也尝试过使用 xml。

    <LinearLayout
        android:id="@+id/header_llt"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size_100"
        android:background="@color/action_bar_color"
        android:elevation="3dp"
        android:orientation="vertical">

        <com.newActivity.views.UbuntuMediumTextView
            android:id="@+id/header_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="@dimen/margin_10"
            android:text="@string/select_country_hint_text"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_18"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/inputSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_10"
            android:layout_marginLeft="@dimen/margin_10"
            android:layout_marginRight="@dimen/margin_10"
            android:drawableLeft="@drawable/abc_ic_search_api_mtrl_alpha"
            android:drawablePadding="@dimen/view_padding_normal"
            android:hint="@string/search_country_hint_text"
            android:inputType="textVisiblePassword"
            android:textColor="@color/white"
            android:textColorHighlight="@color/white_transparent"
            android:textColorHint="@color/white_transparent"
            android:textSize="@dimen/text_size_16" />

    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"></ListView>

    <TextView
        android:id="@+id/empty_tv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/no_result_found"
        android:textColor="@color/text_color_gray_light"
        android:textSize="@dimen/text_size_16"
        android:visibility="gone">

    </TextView>

</LinearLayout>

添加空视图后有什么方法可以保留列表视图的宽度。需要一些帮助。谢谢

【问题讨论】:

  • 尝试将布局参数设置为列表视图

标签: android listview listadapter empty-list


【解决方案1】:

在 XML 中设置它们,如下所示:

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

    <ListView
        android:id="@+id/mainListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/emptyListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="THE LIST IS EMPTY!"
        android:visibility="gone" />

</LinearLayout>

然后:

ListView yourListView = (ListView) findViewById(R.id.yourListView);
yourListView.setEmptyView(findViewById(R.id.emptyListView));

【讨论】:

  • 我也试过这个。但同样的结果。我的列表在 dialogfragment 内
  • 这绝对可以。我怀疑您使用了不正确的 layout_width/height 值。发布您的 XML 代码会有所帮助。
【解决方案2】:

我认为您需要更改对话框的布局参数,将其添加到您的对话框片段代码中

@Override
public void onStart()
{
    super.onStart();

    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int height = (int) (getResources().getDisplayMetrics().heightPixels * 0.70);
        int width = (int) (getResources().getDisplayMetrics().widthPixels * 0.80);
        dialog.getWindow().setLayout(width, height);
    }
}

【讨论】:

    【解决方案3】:

    如果你想在没有数据时显示空视图,你应该将空视图设置为列表视图

    <TextView android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No Results" />
    
    
    listView.setEmptyView(findViewById(android.R.id.empty));
    

    【讨论】:

    • 注意android:id="@android:id/empty"
    【解决方案4】:

    来自Android Documentation

    View.GONE 这个视图是不可见的,它不占用任何空间用于布局。

    View.INVISIBLE 这个视图是不可见的,但它仍然占用空间用于布局。

    因此,如果您将android:visibility="gone" 更改为android:visibility="invisible",视图将不可见,但仍会占用空间。

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多