我试着从Android UI设计的幻灯片中克隆一个活动的设计。然而,我有一个非常简单的任务的问题。
我已经创建了如图所示的布局,头部是一个RelativeLayout中的TextView。现在我想改变相对布局的背景颜色,但我似乎不知道该怎么做。
我知道我可以在XML文件中的RelativeLayout标签中设置android:background属性,但我该将其设置为什么呢?我想定义一个新的颜色,我可以在多个地方使用。它是可绘制的还是字符串?
此外,我希望有一个非常简单的方法来从Eclipse Android UI设计器,我必须错过?
我现在有点沮丧,因为这应该是一个最多只需点击几下就能执行的活动。所以任何帮助都是非常感激的。:)
4种可能的方法,用你需要的。
1. Kotlin
val ll = findViewById<LinearLayout>(R.id.your_layout_id)
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
2. 数据绑定
<LinearLayout
android:background="@{@color/white}"
或者更有用的语句-
<LinearLayout
android:background="@{model.colorResId}"
3.XML
<LinearLayout
android:background="#FFFFFF"
<LinearLayout
android:background="@color/white"
4. Java
LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));