如何改变背景颜色在安卓应用程序

我希望能够在我的android应用程序中以最简单的方式将背景颜色更改为白色。

863475 次浏览

你需要使用android:background属性,例如

android:background="@color/white"

此外,您还需要在strings.xml中为white添加一个值

<color name="white">#FFFFFF</color>

编辑:2012年11月18日

8字母颜色代码的前两个字母提供alpha值,如果你使用html 6字母颜色符号,颜色是不透明的。

例如:

enter image description here

你也可以使用

android:background="#ffffff"

在你的xml布局或/res/layout/activity_main.xml中,或者你可以在AndroidManifest.xml中通过添加

android:theme="@android:style/Theme.Light"

到你的活动标签。

如果您想动态更改背景,请使用

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));

简单的方法

android:background="@android:color/white"

不需要定义任何东西。它使用android.R中预定义的颜色。

最简单的方法是在layout.xml或/res/layout/activity_main.xml的主节点中添加android:background="#FFFFFF":

<?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="fill_parent"
android:padding="10dp"
android:textSize="20sp"
android:background="#FFFFFF">
</TextView>

以最简单的方式通过编程改变背景颜色(仅不改变XML):

LinearLayout bgElement = (LinearLayout) findViewById(R.id.container);
bgElement.setBackgroundColor(Color.WHITE);

唯一的要求是activity_whatever.xml中的“base”元素有一个你可以在Java中引用的id(在本例中是container):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</LinearLayout>

Paschalis和James在这里回答,在检查了如何设置TextView的文本颜色在代码?中的各种可能性后,有点让我想到了这个解决方案。

希望它能帮助到一些人!

你可以试试这种方法

android:background="@color/white"

这个方法对我很管用:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.layout.rootLayout);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.bg_color_2));

在布局xml中设置id

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="@color/background_color"

添加颜色值/color.xml

<color name="bg_color_2">#ffeef7f0</color>

另一种方法,去布局 ->你的. xml文件 -> 设计视图。然后去组件树,选择你想改变颜色的布局。在下面的组件树中有属性部分。在属性部分中选择背景(在图1部分中)。然后单击图2部分。然后资源窗口将打开。从该菜单中选择颜色。然后选择你想要的颜色。__abc8

你可以在xml表中尝试:

android:background="@color/background_color"

在布局中,改变背景使用这个。

android:background="@color/your_color"

在程序中可以使用这个。例如:Texview背景色

 TextView tvName = (TextView) findViewById(R.id.tvName);
tvName.setBackgroundColor(getResources().getColor(R.color.your_color));

你可以使用简单的颜色资源,通常在里面指定

res/values/colors.xml.

使用

<color name="red">#ffff0000</color>

并通过android:background="@color/red"来使用它。这种颜色也可以用于其他任何地方,例如作为文本颜色。以同样的方式在XML中引用它,或者在代码中通过

getResources().getColor(R.color.red).

你也可以使用任何可绘制资源作为背景,使用android:background="@drawable/mydrawable"(这意味着9patch可绘制对象,普通位图,形状可绘制对象,..)。

有时,文本与背景颜色相同,尝试用android:background="#CCCCCC"进入listview属性,你会看到。

< >强android:背景= " # 64 b5f6”
您可以根据自己的规范或需要更改'#'后的值,这取决于您想如何使用它们。
下面是一个示例代码:

<TextView
android:text="Abir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:background="#D4E157"  />

谢谢!

  1. 访问Activity_Main.xml
  2. 有设计视图/和文本视图。
  3. 选择文本视图
  4. 写这个代码:

    android:background="@color/colorAccent"
    
使用渐变背景的最好方法,因为它不会增加应用程序的大小,图像是android应用程序的毒药,所以尽量少使用它,而不是使用一种颜色作为背景,你可以在一个背景中使用多种颜色。 enter image description here enter image description here < / p >

enter image description hereenter image description here

如果你想为整个活动添加背景色

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1de9b6"
tools:context="com.example.abc.myapplication.MainActivity">
</RelativeLayout>

如果你想使用背景视图

 <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Andrios"
android:background="@color/colorAccent" />

希望这能有所帮助!

这段代码对你很有用:

android:background="#fff"

我想能够改变背景颜色为白色在我的

题目说的是最简单的方法,所以在这里。

在你所有的父视图中设置parentViewStyle。像大多数家长视图你的活动,片段和对话框。

<LinearLayout style="@style/parentViewStyle">


... other components inside


</LinearLayout>

只要把这个样式放在res>values>styles.xml里面

<style name="parentViewStyle">
<item name="android:layout_height">match_parent</item>
<item name="android:layout_width">match_parent</item>
<item name="android:background">@color/white</item> // set your color here.
<item name="android:orientation">vertical</item>
</style>

通过这种方式,你不需要在未来改变背景颜色很多次。

对于芬兰湾的科特林和not only,当你写

@color /

你可以选择任何你想要的,快速而简单:

android:background="@color/md_blue_900"

change_color.setBackground(getDrawable(R.color.purple_700));

当我尝试在点击时改变颜色时,这种方法是有效的。