编程改变布局的颜色布局 [英] Programatically change the layout color of layout

查看:156
本文介绍了编程改变布局的颜色布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式改变布局的颜色,但相对布局(试过线性布局,但并没有改变),但不能改变它。

也尝试调试应用程序没有帮助,有没有与我的TAG信息。

应用站定布局最初有色之后。

 包com.test.intentdemo;进口android.app.Activity;
进口android.graphics.Color;
进口android.os.Bundle;
//进口android.os.SystemClock;
进口android.widget.RelativeLayout;
导入android.util *。
进口java.lang.Thread中;公共类intentDemo延伸活动{
    / **当第一次创建活动调用。 * /
    RelativeLayout的lLayout;
    公共静态最后的字符串标记=MyActivity;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        lLayout =(RelativeLayout的)findViewById(R.layout.main);
        如果(Log.isLoggable(TAG,0))
        {
            Log.e(TAG错误之前);
            Log.i(TAG,INFO之前);
            Log.d(TAG,DEBUG之前);                lLayout.setBackgroundColor(Color.parseColor(#000000));
                //SystemClock.sleep(2000);
                尝试
                {
                Thread.currentThread();
                视频下载(2000);
                }
                赶上(例外五)
                {
                //e.message();
                }
            Log.e(TAG错误之后);
            Log.i(TAG,INFO AFTER);
            Log.d(TAG,DEBUG AFTER);
        }
    }
}

任何有关此帮助将是有益的。


解决方案

  lLayout =(RelativeLayout的)findViewById(R.layout.main);

这是错误的。 findViewById 预计的查看的ID。所以,给一个ID为 RelativeLayout的,例如:

 < RelativeLayout的
    机器人:ID =@ + ID / the_id

然后:

  lLayout =(RelativeLayout的)findViewById(R.id.the_id);


 进口android.app.Activity;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.widget.RelativeLayout;公共类intentDemo延伸活动{
    公共静态最后的字符串标记=MyActivity;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        RelativeLayout的lLayout =(RelativeLayout的)findViewById(R.layout.the_id);
        lLayout.setBackgroundColor(Color.parseColor(#000000));
    }
}

I am trying to programatically change the layout color but of a relative layout (tried Linear layout but didn't change), but cannot change it.

Also trying to debug the app doesn't help, there was not message related to my TAG.

the application stood still after layout was colored initially.

package com.test.intentdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
//import android.os.SystemClock;
import android.widget.RelativeLayout;
import android.util.*;
import java.lang.Thread;

public class intentDemo extends Activity {
    /** Called when the activity is first created. */
    RelativeLayout lLayout;
    public static final String TAG="MyActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lLayout = (RelativeLayout) findViewById(R.layout.main);
        if (Log.isLoggable(TAG,0))
        {
            Log.e(TAG,"ERROR BEFORE");
            Log.i(TAG,"INFO BEFORE");
            Log.d(TAG,"DEBUG BEFORE");

                lLayout.setBackgroundColor(Color.parseColor("#000000"));
                //SystemClock.sleep(2000);
                try
                {
                Thread.currentThread();
                Thread.sleep(2000);
                }
                catch (Exception e)
                {
                //e.message();
                }


            Log.e(TAG,"ERROR AFTER");
            Log.i(TAG,"INFO AFTER");
            Log.d(TAG,"DEBUG AFTER");
        }
    }
}

Any help regarding this would be helpful.

解决方案

lLayout = (RelativeLayout) findViewById(R.layout.main);

This is wrong. findViewById expects an id of a View. So, give an ID to RelativeLayout, for instance:

<RelativeLayout
    android:id="@+id/the_id"

Then:

lLayout = (RelativeLayout) findViewById(R.id.the_id);


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RelativeLayout;

public class intentDemo extends Activity {
    public static final String TAG="MyActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RelativeLayout lLayout = (RelativeLayout) findViewById(R.layout.the_id);
        lLayout.setBackgroundColor(Color.parseColor("#000000"));
    }
}

这篇关于编程改变布局的颜色布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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