-1

I'm learning how to develop Android application and am trying to add an image and a text centered on the screen if the ListView is empty. I've used the FrameLayout but it didn't work. I could use a margem Top, but this would cause a cut in the landscape mode. any idea?

Search (View.GONE)
Search (View.VISIBLE)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/searchLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edtPesquisa"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:hint="@string/pesquisa_txt"
            android:inputType="textPersonName"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/idLayoutActVirabrequins"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <com.google.android.gms.ads.AdView
            android:id="@+id/idViewActVirabrequins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="2dp"
            android:background="@drawable/ads"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id_act_virabrequins">
        </com.google.android.gms.ads.AdView>

        <ListView
            android:id="@+id/lstVirabrequins"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="1dp"
            android:longClickable="true"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ViewStub
            android:id="@+id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout="@layout/empty" />

    </LinearLayout>

</LinearLayout>

it is empty layout

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:alpha="0.7"
        android:src="@drawable/empty"
        android:contentDescription="@string/app_name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="#757575"
        android:alpha="0.7"
        android:textSize="16sp"
        android:fontFamily="sans-serif-light"
        android:text="@string/empty" />

</LinearLayout>

thanks in advance!

4

2 回答 2

0

错误...

首先,我没有在主布局的高度中使用“match_parent”。最后我没有使用 weight="1" 的 FrameLayou

图片和文字在中心:)

现在,它就像一个魅力

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" <--------------
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/searchLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edtPesquisa"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:hint="@string/pesquisa_txt"
            android:inputType="textPersonName"
            android:textSize="16sp" />

    </LinearLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/idViewActVirabrequins"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="2dp"
        android:background="@drawable/ads"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id_act_virabrequins">
    </com.google.android.gms.ads.AdView>

    <FrameLayout <-----------
        android:id="@+id/idLayoutActVirabrequins"
        android:layout_width="match_parent"
        android:layout_height="0dp" <------------
        android:layout_weight="1"> <------------

        <ListView
            android:id="@+id/lstVirabrequins"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="1dp"
            android:longClickable="true"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ViewStub
            android:id="@+id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout="@layout/empty" />

    </FrameLayout>

</LinearLayout>
于 2015-09-30T16:41:38.447 回答
0

ListView有一个内置的方法。尝试调用listView.setEmptyView(),并传递您为此创建的视图。

于 2015-09-30T13:46:30.410 回答