【发布时间】:2017-05-20 05:47:52
【问题描述】:
我正在尝试获取 ViewGroup 的颜色并尝试与资源颜色进行比较。
这是我的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/mainLayout"
android:background="@color/colorPrimary"
android:layout_height="match_parent"
>
在我的代码中我正在这样做。
ViewGroup mainLayout = (ViewGroup) findViewById(R.id.mainLayout);
ColorDrawable viewColor = (ColorDrawable) mainLayout.getBackground();
if (viewColor != null)
{
int colorId = viewColor.getColor(); // i get -6371612
int mainColor = getResources().getColor(R.color.colorPrimary); // i get -65536
if (colorId == mainColor)
{
mainLayout.setBackgroundColor(Color.RED);
}
}
colorId 和 mainColor 不相同。我错过了什么?他们不应该一样吗?
【问题讨论】:
标签: java android android-studio material-design