以编程方式更改布局的布局颜色 [英] Programmatically change the layout color of layout

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

问题描述

我试图以编程方式更改布局颜色,但是更改为相对布局(尝试使用线性布局,但未更改),但是无法更改.

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.

尝试调试应用程序也无济于事,没有与我的TAG相关的消息.

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

推荐答案

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

这是错误的. findViewById的ID为View.因此,请给RelativeLayout一个ID,例如:

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

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

然后:

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天全站免登陆