在Android中更改LinearLayout的背景

Dur*_*rga 49 xml android android-linearlayout

我正在开发一个Android应用程序.我想更改LinearLayout元素的背景.

我可以设置什么属性来更改其背景?

Swa*_* EP 107

如果你想使用android的默认颜色代码设置xml,那么你需要做如下:

android:background="@android:color/white"
Run Code Online (Sandbox Code Playgroud)

如果您的项目中指定了颜色colors.xml,则使用:

android:background="@color/white"
Run Code Online (Sandbox Code Playgroud)

如果您想以编程方式执行,请执行以下操作:

linearlayout.setBackgroundColor(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)


raj*_*raj 25

LinearLayout li=(LinearLayout)findViewById(R.id.layoutid);
Run Code Online (Sandbox Code Playgroud)

为你的布局设置背景颜色.

li.setBackgroundColor(Color.parseColor("#ffff00"));
Run Code Online (Sandbox Code Playgroud)

这是设置您可以存储在可绘制文件夹中的图像

li.setBackgroundDrawable(drwableItem);
Run Code Online (Sandbox Code Playgroud)

一些用于显示目的动画或img的资源

li.setBackgroundResource(R.id.bckResource);
Run Code Online (Sandbox Code Playgroud)


Ayu*_*udh 7

你刚刚使用了属性

  • android:background ="#ColorCode"用于颜色

    如果您的图像保存在drawable文件夹中,则使用: -

  • android:background ="@ drawable/ImageName"用于图像设置