Android method for setting the PreferenceCategory background color

  • 2020-06-19 11:43:57
  • OfStack

This article illustrates how Android sets the background color of PreferenceCategory. Share to everybody for everybody reference. The specific analysis is as follows:

PreferenceCategory has a black background by default, so how do we change the background of PreferenceScreen, so this separator looks ugly, so how do we change the background? We can do this by customizing VIEW.

The code is as follows:


public class MyPreferenceCategory extends PreferenceCategory {
 public MyPreferenceCategory(Context context, AttributeSet attrs) {
  super(context, attrs);
 }
 @Override
 protected void onBindView(View view) {
  super.onBindView(view);
  view.setBackgroundColor(Color.parseColor("#b0000000"));
  if (view instanceof TextView) {
   TextView tv = (TextView) view;
   tv.setTextSize(16);
   tv.setTextColor(Color.BLACK);
  }
 }
}

During the xml call (custom usage... You know) :


<com.blogchen.myview.MyPreferenceCategory android:title=" other " >
  <PreferenceScreen
   android:key="blog_"
   android:summary=" Author's blog address "
   android:title=" Access to the blog " >
   <intent
    android:action="android.intent.action.VIEW"
    android:data="https://www.ofstack.com" />
  </PreferenceScreen>
</com.blogchen.myview.MyPreferenceCategory>

I hope this article has been helpful for your Android programming.


Related articles: