简体   繁体   中英

Listview and Action bar TRANSPARENT in android

I have a listview in my activity and Action bar for activity set to transparent and UP navigation. Results are showing up properly but, the first item is showing up below actionBAR. Like the image below:

查看下面的图片

Below is the code I am using to make the bar transparent:

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    actionBar.setStackedBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setDisplayShowHomeEnabled(false);
    setContentView(R.layout.invite_friends);

invite_friends.XML for listView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bluebackground"
android:orientation="vertical" >

<ListView
    android:id="@+id/person_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

 </LinearLayout>

Surprising that the listview is thinking it is full screen? How to solve this issue?

Thanks!

The problem is this:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

You are telling the ActionBar to be in overlay mode, that means it will overlay the content instead of being at a fixed position above it. This is useful if you want to dynamically hide and show the ActionBar in your app, but it is not required to make the ActionBar transparent. Just remove it and it should work as you expect.

If you however want or need the ActionBar in overlay mode than you can just apply a padding to the content in your Activity which is equal to the ActionBar height. Like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?android:attr/actionBarSize">
    ...
</RelativeLayout>

If you are using the support library you have to use ?attr/actionBarSize insted of ?android:attr/actionBarSize like this:

<!-- Support library compatibility -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    ...
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM