如何将背景设置为纯色?当我使用 setContentView 时,屏幕是空白的

问题描述 投票:0回答:2

我已经看过并尝试了很多不同的事情,但无论我最终做什么,屏幕总是空白的,我确信我正在做的事情真的很愚蠢,我希望有人会发现它。

我正在尝试替换背景颜色,但在我做到这一点之前,我需要获取它,以便即使是一种背景颜色也能正确显示。

首先,我的 xml 布局运行良好,当我进入布局视图时,它会按照我想要的方式显示颜色。当我在调用 xml 的活动中调用 setContentView() 时,它永远不会显示,我只会看到一个空白屏幕。

其次,自从上面描述的这个初始问题以来,我已经尝试了几个修复并相应地对它们进行了编号。当我进行修复时,我通常只是费心将其注释掉,而不是在它不起作用后将其删除。某些行后面有一个数字,因此如果三行后面有 1,那么这些行就是尝试 #1 中使用的三行。

第三,在尝试这些修复时,我添加了一个颜色 xml 文件,同时我也会显示。

最后,我将首先显示我的主要活动,其次显示 xml 文件,最后显示颜色文件。正如你所看到的,我的最终目标是动态改变背景,但我现在甚至无法让它正常工作。仅供参考,我的闪屏工作正常。但这只是一个图像。

感谢您的帮助。

public class Blink extends Activity {
    long startTime= System.currentTimeMillis();
    long now=0;//the current time in millis

public void OnCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //TextView backgroundColor=new TextView(this);2,3,4,5,6
    //backgroundColor.setBackgroundColor(0xFFFF0000);5
    //backgroundColor.setBackgroundResource(R.color.royalBlue);2,3,4
    //backgroundColor.setVisibility(0);//make visible 3
    setContentView(R.layout.blank);1
    //setContentView(backgroundColor);4,5,6
    //backgroundColor.setBackgroundColor(Color.argb(255, 255, 255, 255));6

            //setContentView(R.layout.blink_blue);
    //blink from royal blue to blank
            /*while(true){
        startTime= System.currentTimeMillis();
        do{
            now=System.currentTimeMillis();
            setContentView(R.layout.blink_blue);
        }while((-(startTime-now))>1000);

        do{
            now=System.currentTimeMillis();
            setContentView(R.layout.blank);
        }while((-(startTime-now))>1000);
    }*/
}

这开始了 xml 文件

//it is formatted properly but for some reason stack overflow doesn't like it so I'm only posting relevant lines. 

//This is a Linear layout
android:id="@+id/blinkBlue"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/royalBlue"

这开始了颜色文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="royalBlue">#4169e1</color>//Yes I have tried #FF4169e1 instead
    <color name="plainBlue">#ff000000</color> 
    <color name="darkBlue">#ff000000</color>
    <color name="black">#00000000</color>
    <!-- I also know that the blues here aren't those colors... I'll change that when I fix this thing. -->
</resources>
android screen
2个回答
0
投票

您不能以这种方式设置颜色,您可以指定在 XML 中定义的

Layouts
的颜色,然后您
setContentView()
到该 XML 文件。

例如,假设您的 XML 文件的名称为

my_layout.xml
,然后您已在
my_color.xml
中指定了颜色,因此您可以这样操作:

  • 您在
    my_layout.xml
  • 中编写布局
  • 现在您按照上面的方式编写颜色的资源 XML,并将其保存在
    /res/values/my_color.xml
  • 将布局(在
    my_layout.xml
    中定义)背景设置为 `android:background="@color/my_color"
  • 在您的代码中,使用
    setContentView(R.layout.my_layout)

这将使

my_layout.xml
成为您内容的布局,并且背景颜色将由
my_layout.xml
文件内的布局处理。

希望有帮助。


0
投票

看一下使用状态列表可绘制作为背景。每个项目都可以指向指定不同背景颜色的可绘制形状。或者,使用可绘制的形状作为背景,该背景指向颜色状态列表作为纯色。

如果可用于定义可绘制状态列表或颜色状态列表的内置属性不适合您的应用程序,您可以使用本线程中所示的技术来定义您自己的。

© www.soinside.com 2019 - 2024. All rights reserved.