【问题标题】:Changing the background image of a Activity which extends ListActivity更改扩展 ListActivity 的 Activity 的背景图像
【发布时间】:2011-10-18 11:16:06
【问题描述】:

我按照以下教程进行操作:http://mobile.tutsplus.com/tutorials/android/android-listview/ 并能够成功运行该应用程序。它主要使用一个名为list_item.xml的xml文件,其内容是

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp" />

在一个名为 TutListActivity.java 的文件中,这个 xml 被加载为:

public class TutListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
            R.array.tut_titles, R.layout.list_item));
}}

这是首次启动应用程序时加载的第一个活动。我的要求是更改完整屏幕的背景视图。当我在 xml 文件 list_item.xml 中尝试 android:background=@drawable/paperImage 时,我能够更改列表中每一行的背景图像,而不是完整的视图。谁能帮帮我?

【问题讨论】:

  • 你在哪里设置你的内容视图,比如 setContentView(R.layout.some_name);像这样的吗?
  • 一开始我也很困惑,但是在我在问题开头发布的教程链接中,作者提到:n TutListActivity.java,修改onCreate()方法以使用setListAdapter () 方法来加载数据。与常规活动不同,对于整个活动只是一个 ListView 的情况,ListActivity 不需要使用 setContentView()。

标签: android listview android-layout textview android-linearlayout


【解决方案1】:

只需在 setListAdapter 之后使用下面写的以下几行和您的图像 id 我希望这是您想要的

ListView lv = getListView();
    lv.setCacheColorHint(0);
    lv.setBackgroundResource(R.drawable.myMainBackground);

【讨论】:

    【解决方案2】:

    您为列表视图指定了 2 个 xml 布局。 1 用于活动,另一个用于列表视图的一行。您应该设置用于活动的布局的背景,即您放入 setContentView 的布局。现在您正在将背景图像设置为行 xml 文件。

    【讨论】:

    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2016-03-23
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多