【发布时间】:2011-02-14 11:37:21
【问题描述】:
我希望能够以最简单的方式在我的 Android 应用程序中将背景颜色更改为白色。
【问题讨论】:
标签: android
我希望能够以最简单的方式在我的 Android 应用程序中将背景颜色更改为白色。
【问题讨论】:
标签: android
change_color.setBackground(getDrawable(R.color.purple_700));
当我尝试在点击时更改颜色时,这种方法很有效。
【讨论】:
对于 Kotlin 而不仅仅是在你编写时
@color/
你可以选择任何你想要的东西,快速而简单:
android:background="@color/md_blue_900"
【讨论】:
我希望能够在我的 以最简单的方式使用 android 应用程序。
在所有父视图中设置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>
这样以后就不用多次更换背景颜色了。
【讨论】:
此代码可能对您有用:
android:background="#fff"
【讨论】:
如果您想为整个活动添加背景颜色
<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" />
希望这会有所帮助!
【讨论】:
您可以在xml 表中试试这个:
android:background="@color/background_color"
【讨论】:
写下这段代码:
android:background="@color/colorAccent"
【讨论】:
android:background="#64B5F6"
您可以根据自己的规范或需要更改“#”后的值,具体取决于您要如何使用它们。
这是一个示例代码:
<TextView
android:text="Abir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:background="#D4E157" />
谢谢你:)
【讨论】:
有时,文本与背景颜色相同,尝试将 android:background="#CCCCCC" 放入 listview 属性中,您会看到。
【讨论】:
你可以使用简单的颜色资源,通常在里面指定
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="@color/your_color"
在程序中可以使用这个。例如:Texview 背景颜色
TextView tvName = (TextView) findViewById(R.id.tvName);
tvName.setBackgroundColor(getResources().getColor(R.color.your_color));
【讨论】:
android:background="@android:color/white"
无需定义任何东西。它使用android.R 中的预定义颜色。
【讨论】:
context.getResources().getColor(android.R.color.white)
另一种方式,转到 layout -> 你的 .xml 文件 -> 设计视图 。然后转到 组件树 em> 并选择要更改颜色的布局。在下面的组件树中有 properties 部分。在属性部分中选择 background(在图片部分 1 中)。然后点击图片中的第 2 部分。然后 Resources 窗口将打开。从那个选择 color 菜单。然后选择你想要的颜色。enter image description here
【讨论】:
这个方法对我有用:
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>
【讨论】:
你可以试试这个方法
android:background="@color/white"
【讨论】:
以最简单的方式以编程方式更改背景颜色(仅 - 不更改 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? 中的各种可能性后,有点引导我找到了这个解决方案。
希望它对某人有所帮助!
【讨论】:
你也可以使用
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));
【讨论】:
最简单的方法是在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>
【讨论】:
你需要使用 android:background 属性,例如
android:background="@color/white"
您还需要在 strings.xml 中为 white 添加一个值
<color name="white">#FFFFFF</color>
编辑:2012 年 11 月 18 日
8 字母颜色代码的前两个字母提供 alpha 值,如果您使用 html 6 字母颜色表示法,则颜色是不透明的。
例如:
【讨论】:
android:background="@android:color/white" 更容易,不需要您向strings.xml 添加任何内容。
/res/layout/activity_main.xml 以将元素android:background 添加到答案中?