Java 如何在android studio中更改整个背景

Java 如何在android studio中更改整个背景,java,android,xml,listview,bluetooth,Java,Android,Xml,Listview,Bluetooth,在我的design render中,它显示整个背景是蓝色的,但实际上应用程序在ListView上只有颜色。下面是我的xml代码和屏幕截图 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent"

在我的design render中,它显示整个背景是蓝色的,但实际上应用程序在ListView上只有颜色。下面是我的xml代码和屏幕截图

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#b8569fee">

    <TextView android:id="@+id/device_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        android:background="#b8569fee"
            android:textSize="24dp"/>

    <TextView
        android:id="@+id/device_address"
        android:layout_width="match_parent"
        android:background="#b8569fee"
        android:layout_height="wrap_content"
        android:textSize="12dp" />
</LinearLayout>

渲染

实际应用程序:

我试图更改textview布局高度以匹配父项,但仍然无效,因此我需要做什么来更改整个背景


Thnaks

这里发生的是,您的XML文件正在为列表视图中的单个元素定义布局,然后列表视图以编程方式应用此XML,以便在发现新的BLE设备时在列表上构建新视图。每个列表元素的大小由外部定义,由将每个视图放入列表的内容决定。因此,当您将match_parent放入LinearLayout并期望它能够解决您的问题时,情况并非如此,因为它只填充listview分配的区域。修复方法是在listview本身中更改背景颜色,这在XML中是不做的

我需要查看更多的代码才能确切地知道如何做到这一点,但我将在黑暗中进行尝试,并假设您正在使用ListActivity,如中所示。在这种情况下,您可以使用以下方法更改ListView的背景色:

getListView().setBackgroundColor(Color.rgb(86, 159, 238));

放置在onCreate方法中。

这里发生的事情是,您的XML文件正在为列表视图中的单个元素定义布局,然后列表视图以编程方式应用此XML,以便在发现新设备时在列表上构建新视图。每个列表元素的大小由外部定义,由将每个视图放入列表的内容决定。因此,当您将match_parent放入LinearLayout并期望它能够解决您的问题时,情况并非如此,因为它只填充listview分配的区域。修复方法是在listview本身中更改背景颜色,这在XML中是不做的

我需要查看更多的代码才能确切地知道如何做到这一点,但我将在黑暗中进行尝试,并假设您正在使用ListActivity,如中所示。在这种情况下,您可以使用以下方法更改ListView的背景色:

getListView().setBackgroundColor(Color.rgb(86, 159, 238));

放置在onCreate方法中。

所示视图仅为布局内包含
列表视图的视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b8569fee">

    <TextView android:id="@+id/device_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#b8569fee"
        android:textSize="24dp"/>

    <TextView
        android:id="@+id/device_address"
        android:layout_width="match_parent"
        android:background="#b8569fee"
        android:layout_height="wrap_content"
        android:textSize="12dp" />
</LinearLayout>

所示视图仅为布局内包含
列表视图的视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b8569fee">

    <TextView android:id="@+id/device_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#b8569fee"
        android:textSize="24dp"/>

    <TextView
        android:id="@+id/device_address"
        android:layout_width="match_parent"
        android:background="#b8569fee"
        android:layout_height="wrap_content"
        android:textSize="12dp" />
</LinearLayout>

是否要更改视图或整个背景的颜色?@BOB显示包含listView的布局,请参阅我的答案。您在listView的模板中编写了该代码,而不是在显示列表视图的活动的实际XML中编写的代码。请改为添加`android:background=“#b8569fee”“在@Rohit B的内部background@Jorgesys我没有listView的其他布局。是否要更改视图或整个背景的颜色?@BOB显示包含listView的布局,请看我的答案。您在ListView的模板中编写了该代码,而不是在显示列表视图的活动的实际XML中。而是在@Rohit B中添加`android:background=“#b8569fee”`作为整体background@Jorgesys我没有listView的其他布局。它成功了,谢谢。您介意指出在源代码中它在哪里设置每个列表元素的大小吗?如果我想使用xml文件来更改背景,我需要更改什么?这并不容易,因为您使用的是ListActivity,布局是由Google编写的源代码定义的。当然,您可以通过在OnCreate中使用setContentView来定义自己的XML供其使用,从而覆盖ListActivity的布局。请看关于它工作的部分,谢谢。您介意指出在源代码中它在哪里设置每个列表元素的大小吗?如果我想使用xml文件来更改背景,我需要更改什么?这并不容易,因为您使用的是ListActivity,布局是由Google编写的源代码定义的。当然,您可以通过在OnCreate中使用setContentView来定义自己的XML供其使用,从而覆盖ListActivity的布局。请参阅上的一节