我如何将背景设置为纯色?当我使用的setContentView屏幕是空白 [英] How do I set the background to a solid color? When I use setContentView the screen is blank

查看:688
本文介绍了我如何将背景设置为纯色?当我使用的setContentView屏幕是空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过并尝试了很多不同的东西,但不管我最终会做屏幕始终是空白,我敢肯定这件事情非常愚蠢,我做什么,我希望有人会抓住它。

我试图替代的背景颜色,但之前,我什至认为我需要得到它,这样连一个背景颜色会正确显示。

首先,我的XML布局工作正常,当我到布局来看它显示,就像我希望它的颜色。当我去的setContentView()的调用它永远不会显示在XML中的活动,我只得到一个空白屏幕。

第二,因为我上面描述的这个初始发行已经尝试了一些修复,并相应地编号他们。当我做了修复我通常只不屑于它注释掉,而不是删除后没有工作的。某些行之后有一个号码,因此,如果三线,在其后面1的那么这些均试图1#所使用的三行。

三,尝试这些修补程序我增加了一个颜色的XML文件,而我将显示为好。

最后,我会告诉我的主要活动首先,XML文件第二,和颜色的文件最后。正如你可以看到我的最终目标是动态地改变背景,但我甚至不能得到它通常现在的工作。和FYI我闪屏工作正常。但是,这是一个图像。

感谢您的帮助。

 公共类眨眼延伸活动{
    长STARTTIME = System.currentTimeMillis的();
    现在长= 0; //当前在米利斯时间公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    // TextView中的backgroundColor =新的TextView(本); 2,3,4,5,6
    //backgroundColor.setBackgroundColor(0xFFFF0000);5
    //backgroundColor.setBackgroundResource(R.color.royalBlue);2,3,4
    //backgroundColor.setVisibility(0);//make可见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);
    //从皇家蓝色空白眨眼
            / *而(真){
        STARTTIME = System.currentTimeMillis的();
        做{
            现在= System.currentTimeMillis的();
            的setContentView(R.layout.blink_blue);
        }而(( - (startTime时现在))> 1000);        做{
            现在= System.currentTimeMillis的();
            的setContentView(R.layout.blank);
        }而(( - (startTime时现在))> 1000);
    } * /
}

此开始xml文件

  //这是正确的格式,但由于某种原因堆栈溢出不喜欢它,所以我只张贴相关线路。//这是一个线性布局
机器人:ID =@ + ID / blinkBlue
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@色/红深蓝

这开始的颜色文件

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
    <颜色名称=红深蓝>#4169e1< /彩色> //是的,我已经试过#FF4169E1代替
    <颜色名称=plainBlue>#FF000000< /彩色>
    <颜色名称=darkBlue>#FF000000< /彩色>
    <颜色名称=黑>#00000000 LT; /彩色>
    <! - 我也知道,这里的蓝调不是那些颜色...我会改变,当我解决这件事情。 - >
< /资源>


解决方案

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

如果可以定义一个状态列表绘制或彩色状态列表内置的属性不适合你的应用程序,你可以使用所示的技术的在这个线程来定义自己。

I have looked and tried a lot of different things but no matter what I end up doing the screen is always blank and I'm sure it's something really dumb I'm doing and I'm hoping someone will catch it.

I'm trying to alternate background colors but before I even get to that I need to get it so that even one background color will display properly.

First, my xml layout works fine and when I got to the layout view it displays the color just as I want it to. When I go to setContentView() in the activity that calls the xml it is never displayed and I only get a blank screen.

Second, since this initial issue described above I have tried several fixes and have numbered them accordingly. As I did a fix I usually only bothered to comment it out instead of deleting it after it didn't work. After certain lines there is a number, so if three lines have 1's behind them then those were the three lines used in attempt #1.

Third, while trying these fixes I added a colors xml file while I'll display as well.

Finally, I'll show my main activity first, xml file second, and colors file last. As you can see my ultimate goal would be to change the background dynamically but I can't even to get it to work normally right now. And FYI my splash screen works fine. But that's an image.

Thanks for you help.

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);
    }*/
}

This begins the xml file

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

This begins the colors file

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

解决方案

Take a look at using a state list drawable as a background. Each item can point to a shape drawable that specifies a different background color. Alternatively, use a shape drawable as a background that points to a color state list as the solid color.

If the built-in attributes available for defining a state list drawable or a color state list are not right for your application, you can use the technique shown in this thread to define your own.

这篇关于我如何将背景设置为纯色?当我使用的setContentView屏幕是空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆