【问题标题】:how to change the entire background in android studio如何在android studio中更改整个背景
【发布时间】:2017-09-06 19:47:24
【问题描述】:

Open source code that I am using

在我的设计渲染中,它向我显示整个背景是蓝色的,但实际的应用程序只有列表视图上的颜色。以下是我的 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布局高度更改为match_parent,但还是不行,那么我需要做些什么来改变整个背景呢?

感谢

【问题讨论】:

  • 您要更改视图或整个背景的颜色?
  • @BOB 显示包含 listView 的布局,请参阅我的答案。
  • 您在 ListView 的模板中编写了该代码,而不是在显示 List View 的活动的实际 XML 中。而是在
  • @Rohit B 整个背景
  • @Jorgesys 我没有其他的 listView 布局。

标签: java android xml listview bluetooth


【解决方案1】:

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

我需要查看更多代码才能确切知道如何执行此操作,但我将在黑暗中试一试,并假设您正在使用 ListActivity,如 Google BluetoothLeGatt example 中所示。在这种情况下,您可以使用以下方式更改 ListView 的背景颜色:

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

放置在onCreate方法中。

【讨论】:

  • 成功了,谢谢。您介意指出它在源代码中的哪个位置设置每个列表元素的大小吗?如果我想使用xml文件来改变背景,我需要改变什么?
  • 没那么简单,既然你用的是ListActivity,布局是由谷歌写的源代码定义的。当然,您可以通过在 OnCreate 中使用 setContentView 来覆盖 ListActivity 的布局,以定义您自己的 XML 供它使用。请参阅Screen Layout here 部分
【解决方案2】:

显示的视图只是布局内包含ListView:的视图

<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>

您必须将背景颜色更改为包含ListView的布局

 android:background="#b8569fee"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多