在 Activity 或 Fragment 中调用自定义颜色(定义在 res/colors.xml 文件中)的方式如下:

1. 自定义颜色文件

确保你的颜色已经在 res/values/colors.xml 文件中定义,例如:

<resources>

 <color name="primaryColor">#FF5722</color>

 <color name="secondaryColor">#4CAF50</color>

 <color name="textColor">#FFFFFF</color>

</resources>

2. 获取自定义颜色

方法 1:使用 ContextCompat.getColor()

这是推荐的方法,适用于大多数场景,兼容不同 API 版本。

在 Activity 中

int primaryColor = ContextCompat.getColor(this, R.color.primaryColor);

在 Fragment 中

int primaryColor = ContextCompat.getColor(requireContext(), R.color.primaryColor);

方法 2:使用 getResources().getColor()

虽然可以使用,但从 API 23 开始被标记为废弃(deprecated),推荐使用 ContextCompat.getColor()。

在 Activity 中

int primaryColor = getResources().getColor(R.color.primaryColor);

在 Fragment 中

int primaryColor = getResources().getColor(R.color.primaryColor);

如果在 API 23+ 环境中,需要传入 Theme 参数:

int primaryColor = getResources().getColor(R.color.primaryColor, getTheme());

3. 应用颜色

设置背景颜色

// Activity 中

View view = findViewById(.some_view);

view.setBackgroundColor(ContextCompat.getColor(this, R.color.primaryColor));

// Fragment 中

View view = getView().findViewById(.some_view);

view.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.primaryColor));

设置文本颜色

TextView textView = findViewById(.some_text_view);

textView.setTextColor(ContextCompat.getColor(this, R.color.textColor));

用于自定义控件或属性

Paint paint = new Paint();

paint.setColor(ContextCompat.getColor(this, R.color.primaryColor));

4. 示例代码

在 Activity 中:

import android.os.Bundle;

import android.widget.TextView;

import .AppCompatActivity;

import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

 @Override

 protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 // 设置背景颜色

 findViewById(.some_view).setBackgroundColor(

 ContextCompat.getColor(this, R.color.primaryColor)

 );

 // 设置文本颜色

 TextView textView = findViewById(.some_text_view);

 textView.setTextColor(ContextCompat.getColor(this, R.color.textColor));

 }

}

在 Fragment 中:

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.core.content.ContextCompat;

import .Fragment;

public class SampleFragment extends Fragment {

 @Nullable

 @Override

 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

 View rootView = inflater.inflate(R.layout.fragment_sample, container, false);

 // 设置背景颜色

 rootView.findViewById(.some_view).setBackgroundColor(

 ContextCompat.getColor(requireContext(), R.color.primaryColor)

 );

 // 设置文本颜色

 TextView textView = rootView.findViewById(.some_text_view);

 textView.setTextColor(ContextCompat.getColor(requireContext(), R.color.textColor));

 return rootView;

 }

}

注意事项

1. 推荐使用 ContextCompat.getColor():

• 兼容性更好,适用于所有 Android API 版本。

2. 确保颜色已定义

• 调用的颜色必须在 colors.xml 中定义,否则会抛出 Resources.NotFoundException。

3. 动态切换颜色

• 如果需要根据主题或运行时动态设置颜色,可以使用 ContextCompat 动态获取颜色资源。

通过这些方法,可以在 Activity 和 Fragment 中轻松调用自定义颜色并应用到界面元素中。