具有空状态的Android列表视图缩小了根视图

时间:2016-06-09 12:02:33

标签: android listview listadapter empty-list

您好我正在开发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>

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

4 个答案:

答案 0 :(得分:0)

将它们设置为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));

答案 1 :(得分:0)

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

public class LocationData {
     //Name these appropriately! I don't know what they're for.
     private Location location;
     private String string1;
     private int num1;
     private String string2;
     private int num2;
     private String string3;
     private String string4;

     //Constructor
     public LocationData(Location loc, String s1, int n1, String s2, int n2, String s3, String s4) {
         location = loc;
         //And so on for each field
         ...
     }

     //One pair of methods for each field
     public Location getLocation() {
         return location;
     }
     public void setLocation(Location loc) {
         location = loc;
     }

答案 2 :(得分:0)

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

<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));

答案 3 :(得分:0)

来自Android Documentation

  

View.GONE此视图不可见,并且不会占用任何空间用于布局。

     

View.INVISIBLE此视图不可见,但仍会占用空间以进行布局。

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