系统设置里面自定义Fragment 设置 PreferenceScreen的标题颜色
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:key="multi_help_menu_main"
android:title="@string/multi_help_menu_main_title">
<Preference
android:key="multi_help_menu_recursos"
android:title="@string/multi_recursos_title"
android:icon="@drawable/ic_multi_recursos"
android:fragment="com.android.settings.multihelpmenu.MultiHelpMenuMultiRecursosFragment" />
<Preference
android:key="multi_help_menu_done"
android:title="@string/multi_help_menu_done_title"
android:icon="@drawable/ic_multi_help_menu_done"
android:fragment="com.android.settings.multihelpmenu.MultiHelpMenuDoneFragment" />
<Preference
android:key="multi_help_menu_question_mark"
android:title="@string/multi_help_menu_question_mark_title"
android:icon="@drawable/ic_multi_help_menu_question_mark"
android:fragment="com.android.settings.multihelpmenu.MultiHelpMenuQuestionFragment" />
<Preference
android:key="multi_help_menu_build"
android:title="@string/multi_help_menu_build_title"
android:icon="@drawable/ic_multi_help_menu_build"
android:fragment="com.android.settings.multihelpmenu.MultiHelpMenuBuildFragment" />
</PreferenceScreen>
---------------------------------自定义Fragment-----------------------------------
package xxx;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.SettingsActivity;
import android.graphics.drawable.Drawable;
import com.android.settings.Utils;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import android.util.Log;
public class MultiHelpMenuFragment extends DashboardFragment {
private static final String TAG = "MultiHelpMenuFragment";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
}
@Override
public int getMetricsCategory() {
return SettingsEnums.SETTINGS_MULTI_HELP_MENU_CATEGORY;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.multi_help_menu_main;
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
final PreferenceScreen screen = getPreferenceScreen();
if (screen == null) {
return;
}
// Tint the homepage icons
final int tintColor = Utils.getHomepageIconColor(getContext());
final int count = screen.getPreferenceCount();
for (int i = 0; i < count; i++) {
final Preference preference = screen.getPreference(i);
if (preference == null) {
break;
}
final Drawable icon = preference.getIcon();
if (icon != null) {
icon.setTint(tintColor);
}
}
}
}
设置title颜色没有,但是没有相关属性;
思路是先看下源码,用的是Androidx,那就继承它重写给view赋值;
androidx.preference_preference.jar
package androidx.preference;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.core.content.res.TypedArrayUtils;
import androidx.preference.PreferenceManager.OnNavigateToScreenListener;
import androidx.preference.R.attr;
public class PreferenceScreen extends PreferenceGroup {
private static final String TAG = "CustomPreferenceScreen";
private boolean mShouldUseGeneratedIds = true;
@RestrictTo({Scope.LIBRARY_GROUP_PREFIX})
public PreferenceScreen(Context context, AttributeSet attrs) {
super(context, attrs, TypedArrayUtils.getAttr(context, attr.preferenceScreenStyle, 16842891));
}
protected void onClick() {
if (this.getIntent() == null && this.getFragment() == null && this.getPreferenceCount() != 0) {
OnNavigateToScreenListener listener = this.getPreferenceManager().getOnNavigateToScreenListener();
if (listener != null) {
listener.onNavigateToScreen(this);
}
}
}
protected boolean isOnSameScreenAsChildren() {
return false;
}
public boolean shouldUseGeneratedIds() {
return this.mShouldUseGeneratedIds;
}
public void setShouldUseGeneratedIds(boolean shouldUseGeneratedIds) {
if (this.isAttached()) {
throw new IllegalStateException("Cannot change the usage of generated IDs while attached to the androidx.preference hierarchy");
} else {
this.mShouldUseGeneratedIds = shouldUseGeneratedIds;
}
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
TextView titleView = (TextView)holder.findViewById(16908310);
Log.i(TAG, "CustomPreferenceScreen titleView: " +titleView);
if (titleView != null) {
titleView.setTextColor(Color.RED);
}
}
}
但是它和普通的preference又有区别,没有走onBindViewHolder方法,所以这个titile不是preference所绑定的layout
通过view布局分析title对应view是哪个(layout inspector 13好像获取不了系统view的布局图)
adb shell dumpsys activity top 分析应该是Toolbar
android.view.ViewStub{db71278 G.E...... ......I. 0,0-0,0 #10201c5 android:id/action_mode_bar_stub}
android.widget.FrameLayout{f639d51 V.E...... ........ 0,0-600,976 #1020002 android:id/content}
androidx.coordinatorlayout.widget.CoordinatorLayout{4fed9b6 V.ED..... ........ 0,0-600,976 #7f0a018a app:id/content_parent
}
com.google.android.material.appbar.AppBarLayout{98e36c2 V.E...... ........ 0,0-600,144 #7f0a0092 app:id/app_bar}
com.google.android.material.appbar.CollapsingToolbarLayout{f9c56b7 V.ED..... ........ 0,0-600,144 #7f0a0173 app:id/col
lapsing_toolbar}
android.widget.Toolbar{86de24 V.E...... ........ 0,24-600,88 #7f0a003f app:id/action_bar}
android.widget.TextView{4f3bd8d V.ED..... ........ 80,18-80,46}
android.widget.ImageButton{796db42 VFED..C.. ........ 8,0-64,64}
android.view.View{915a753 V.ED..... ........ 80,0-584,64}
android.widget.ActionMenuView{465b890 V.E...... ........ 592,0-592,64}
android.widget.FrameLayout{2489cd3 V.E...... ........ 0,144-600,384 #7f0a0189 app:id/content_frame}
最后定位到标题显示在CollapsingToolbarLayout上面, 但是不属于toobar (无法通过setTitleTextColor来改变颜色),通过TextPaint绘制;
+++ core/SettingsBaseActivity.java
import com.google.android.material.resources.TextAppearanceConfig;
import com.google.android.setupcompat.util.WizardManagerHelper;
import com.google.android.setupdesign.util.ThemeHelper;
+import android.widget.TextView;
+import android.graphics.Color;
/** Base activity for Settings pages */
public class SettingsBaseActivity extends FragmentActivity implements CategoryHandler {
return;
}
setActionBar(toolbar);
-
if (DEBUG_TIMING) {
Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
}
@Override
public void setTitle(CharSequence title) {
+ Log.d(TAG, "setTitle "+title);
super.setTitle(title);
if (mCollapsingToolbarLayout != null) {
+ Log.d(TAG, "mCollapsingToolbarLayout setTitle "+title);
mCollapsingToolbarLayout.setTitle(title);
+ mCollapsingToolbarLayout.setExpandedTitleColor(Color.BLUE);
+
}
}
CollapsingToolbarLayout