Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 背景色和背景色是否相同_Android_Android Linearlayout - Fatal编程技术网

Android 背景色和背景色是否相同

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

如果我在xml中设置如下颜色:

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