Добавление фонового изображения в просмотр списка в Android? - PullRequest
3 голосов
/ 17 августа 2011

Я пытаюсь добавить фоновое изображение в listActivity в моем приложении.Однако с кодом, который я использую, фоновое изображение отображается в каждой строке, а не для всего списка.

Это XML-файл:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/qbg1"/>

<TextView
    android:id="@+id/employeeID"
    android:layout_width="50px"
    android:layout_marginRight="20px"
    android:layout_height="wrap_content"
    android:textColor="@color/textColor"/>

<TextView
    android:id="@+id/employeeName"
    android:layout_width="wrap_content"
    android:layout_marginRight="10px"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/employeeID"
    android:textColor="@color/textColor"/>


</RelativeLayout>
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/qbg1"/>

, что является лучшим подходомдобавить фоновое изображение в активность?Я бы предпочел сделать это в файле XML, а не программно.

Ответы [ 3 ]

9 голосов
/ 17 августа 2011

Я решил проблему, добавив фоновое изображение к основному виду, а затем добавил listView внутри этого представления.

Итак, это мой файл main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="4px"
android:background="@drawable/qbg"> 

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
             android:cacheColorHint="#00000000"/> //This is to fix disappearing background issue
</RelativeLayout>

и это мой listView.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px" >

<TextView
android:id="@+id/employeeID"
android:layout_width="50px"
android:layout_marginRight="20px"
android:layout_height="wrap_content"
android:textColor="@color/textColor"/>

<TextView
android:id="@+id/employeeName"
android:layout_width="wrap_content"
android:layout_marginRight="10px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/employeeID"
android:textColor="@color/textColor"/>

</RelativeLayout>

Надеюсь, это будет полезно для других людей.

2 голосов
/ 27 февраля 2013

Вы пытались установить изображение в коде, попробуйте это getListView().setBackgroundResource(R.drawable.matchback);

2 голосов
/ 17 августа 2011

Я использовал что-то подобное, чтобы получить этот эффект.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/paper"
>
<ExpandableListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:fadingEdge="vertical"
android:fadingEdgeLength="10dip"
android:scrollingCache="false"


>
</ExpandableListView>
</RelativeLayout>

Я помню, что мне нужно было настроить некоторые параметры, чтобы фон оставался видимым во время прокрутки списка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...