Android 背景色和背景色是否相同
如果我在xml中设置如下颜色:Android 背景色和背景色是否相同,android,android-linearlayout,Android,Android Linearlayout,如果我在xml中设置如下颜色: <LinearLayout android:id="@+id/borderForList" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/default_border_color" > 颜色不同。这些方法相同吗?不,它们不一样 对于以下各项,它们是等效的: android
<LinearLayout
android:id="@+id/borderForList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/default_border_color" >
颜色不同。这些方法相同吗?不,它们不一样 对于以下各项,它们是等效的:
android:background="@color/default_border_color"
public void setBackgroundResource(int resid)
因此,您可以在xml
或java代码中设置背景资源(drawable
,color
,等等)
至于,似乎没有xml属性(例如android:backgroundcolor
),而是一个公共方法:
public void setBackgroundColor (int color)
试一试
R.color.default\u border\u color是android自动生成的R文件中的一个资源整数,setBackgroundColor()
需要一个颜色整数,因此必须调用getResources().getColor()
方法对其进行转换
看
)
public void setBackgroundColor (int color)
m_border.setBackgroundColor(getResources().getColor(R.color.default_border_color));