Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 設定 PreferenceCategory 背景顏色的方法

編輯:關於 Android 程式設計

本文以實例說明如何設定 Android 中 PreferenceCategory 的背景顏色,供需要的朋友參考,分析如下。

大家可能遇到過這樣的情況:PreferenceCategory 預設是黑色背景,如果我們更換了 PreferenceScreen 的背景,那麼這種分隔欄看起來就很突兀。那要怎麼改背景呢?可以透過自訂 View 來實現。

程式碼如下:

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

在 xml 中呼叫時(自訂用法,你懂的):

 <com.blogchen.myview.MyPreferenceCategory android:title="其他" > <PreferenceScreen android:key="blog_" android:summary="作者博客地址" android:title="訪問博客" > <intent android:action="android.intent.action.VIEW" android:data="https://www.jb51.net" /> </PreferenceScreen> </com.blogchen.myview.MyPreferenceCategory>

希望本文所述能對大家的 Android 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved