【问题标题】:How to change background color in android app如何在android应用程序中更改背景颜色
【发布时间】:2011-02-14 11:37:21
【问题描述】:

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

    当我尝试在点击时更改颜色时,这种方法很有效。

    【讨论】:

      【解决方案2】:

      对于 Kotlin 而不仅仅是在你编写时

      @color/

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

      android:background="@color/md_blue_900"
      

      【讨论】:

        【解决方案3】:

        我希望能够在我的 以最简单的方式使用 android 应用程序。

        问题是最简单的方法,所以就在这里。

        在所有父视图中设置parentViewStyle。就像您的活动、片段和对话框的大多数父视图一样。

        <LinearLayout style="@style/parentViewStyle">
        
          ... other components inside
        
        </LinearLayout>
        

        只要把这个样式放在 res&gt;values&gt;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>
        

        这样以后就不用多次更换背景颜色了。

        【讨论】:

          【解决方案4】:

          此代码可能对您有用:

          android:background="#fff"
          

          【讨论】:

            【解决方案5】:

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

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

            希望这会有所帮助!

            【讨论】:

              【解决方案6】:

              使用渐变背景的最佳方式,因为它不会增加应用图像的应用大小,这对 android 应用来说是毒药,所以尽量少用它,而不是使用一种颜色作为背景,你可以在一个背景中使用多种颜色。

              【讨论】:

                【解决方案7】:

                您可以在xml 表中试试这个:

                android:background="@color/background_color"
                

                【讨论】:

                  【解决方案8】:
                  1. 转到 Activity_Main.xml
                  2. 有设计视图/和文本视图。
                  3. 选择文本视图
                  4. 写下这段代码:

                    android:background="@color/colorAccent"
                    

                  【讨论】:

                    【解决方案9】:

                    android:background="#64B5F6"
                    您可以根据自己的规范或需要更改“#”后的值,具体取决于您要如何使用它们。
                    这是一个示例代码:

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

                    谢谢你:)

                    【讨论】:

                      【解决方案10】:

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

                      【讨论】:

                        【解决方案11】:

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

                        res/values/colors.xml.
                        

                        使用

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

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

                        在代码中获取它
                        getResources().getColor(R.color.red).
                        

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

                        【讨论】:

                          【解决方案12】:

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

                          android:background="@color/your_color"
                          

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

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

                          【讨论】:

                            【解决方案13】:

                            最简单的方法

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

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

                            【讨论】:

                            • 并以编程方式:context.getResources().getColor(android.R.color.white)
                            • 谢谢,这正是我想要的。就我而言,我必须用 Java 来完成。
                            【解决方案14】:

                            另一种方式,转到 layout -> 你的 .xml 文件 -> 设计视图 。然后转到 组件树 em> 并选择要更改颜色的布局。在下面的组件树中有 properties 部分。在属性部分中选择 background(在图片部分 1 中)。然后点击图片中的第 2 部分。然后 Resources 窗口将打开。从那个选择 color 菜单。然后选择你想要的颜色。enter image description here

                            【讨论】:

                              【解决方案15】:

                              这个方法对我有用:

                              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>
                              

                              【讨论】:

                                【解决方案16】:

                                你可以试试这个方法

                                android:background="@color/white"
                                

                                【讨论】:

                                  【解决方案17】:

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

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

                                  唯一的要求是您在 activity_whatever.xml 中的“基础”元素有一个您可以在 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 在查看了 How to set the text color of TextView in code? 中的各种可能性后,有点引导我找到了这个解决方案。

                                  希望它对某人有所帮助!

                                  【讨论】:

                                    【解决方案18】:

                                    你也可以使用

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

                                    【讨论】:

                                    • 这是正确的答案,因为它有主题,我相信。单线不是最简单的方法,而是一种将整个风格传播到所有事物的方法。样式是您通过甚至复杂的更改实现单行的方式。此外,与一般属性不同,样式适用于偏好。 (尽管仍有一些奇怪的地方)
                                    【解决方案19】:

                                    最简单的方法是在layout.xml中的主节点添加android:background="#FFFFFF"或者/res/layout/activity_main.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="fill_parent"
                                           android:padding="10dp"
                                           android:textSize="20sp" 
                                           android:background="#FFFFFF">
                                       </TextView>
                                    

                                    【讨论】:

                                    【解决方案20】:

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

                                    android:background="@color/white"
                                    

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

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

                                    编辑:2012 年 11 月 18 日

                                    8 字母颜色代码的前两个字母提供 alpha 值,如果您使用 html 6 字母颜色表示法,则颜色是不透明的。

                                    例如:

                                    【讨论】:

                                    • @Jonny 前两个字母提供 alpha 值。
                                    • 谢谢,但是使用十六进制值不是更容易吗?
                                    • 这很容易,但是不好的做法:)
                                    • 使用预定义的android:background="@android:color/white" 更容易,不需要您向strings.xml 添加任何内容。
                                    • 您能否添加位置/res/layout/activity_main.xml 以将元素android:background 添加到答案中?
                                    猜你喜欢
                                    • 2011-03-26
                                    • 2017-01-05
                                    • 1970-01-01
                                    • 2018-11-10
                                    • 2020-12-11
                                    • 2020-08-30
                                    • 2021-04-02
                                    • 1970-01-01
                                    • 1970-01-01
                                    相关资源
                                    最近更新 更多