android 修改Layout背景的颜色


代码中通过findViewById()获得Layout可以设置Orientation,但设置背景色无效

通过如下代码也可以修改背景色:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

LinearLayout linearLay = (LinearLayout) layoutInflater.inflate(R.layout.activity_main, null) ;

linearLay.setBackgroundColor(Color.WHITE);

setContentView(linearLay);



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android Studio中,可以使用定时器来修改控件的背景颜色。下面是一个简单的示例代码,演示了如何使用定时器来定时修改控件的背景颜色: 1. 首先,在XML布局文件中定义一个需要修改背景颜色的控件,例如一个Button: ```xml <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change Color" android:background="@color/default_color" /> ``` 2. 在Java代码中,使用Timer和TimerTask类来实现定时任务。在定时任务中,可以通过修改控件的背景颜色来实现效果。以下是示例代码: ```java import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private Button myButton; private Timer timer; private int colorIndex = 0; private int[] colors = {R.color.red, R.color.green, R.color.blue}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButton = findViewById(R.id.myButton); // 创建定时器 timer = new Timer(); // 创建定时任务 TimerTask timerTask = new TimerTask() { @Override public void run() { // 在定时任务中修改控件的背景颜色 runOnUiThread(new Runnable() { @Override public void run() { myButton.setBackgroundResource(colors[colorIndex]); colorIndex = (colorIndex + 1) % colors.length; } }); } }; // 启动定时任务,延迟0毫秒后执行,每1000毫秒执行一次 timer.schedule(timerTask, 0, 1000); } @Override protected void onDestroy() { super.onDestroy(); // 取消定时器 timer.cancel(); } } ``` 在上述代码中,我们使用了一个定时器和一个定时任务。定时任务中通过runOnUiThread方法来修改控件的背景颜色,确保在主线程中进行UI操作。定时任务每隔1秒钟执行一次,循环修改控件的背景颜色。 请注意,上述代码中的colors数组是一个示例,你可以根据自己的需求定义不同的颜色数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值