【问题标题】:Android Preference Fragment Text ColorAndroid偏好片段文本颜色
【发布时间】:2013-02-01 06:53:08
【问题描述】:

我想更改 android 首选项片段中的文本颜色。我目前正在使用自定义主题来更改复选框图像、背景、onClick 突出显示,这一切都很好......除了文本颜色。我不想使用默认主题,我想拥有自己的主题,所以一切看起来都是我想要的,只是更改文本颜色,有人可以帮忙。

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="selectedTextStyle">  
        <item name="android:textSize">18sp</item>
    </style>
    <style name="buttonTextStyle">  
        <item name="android:textSize">20sp</item>
    </style>
    <style name="PreferencesTheme" >
        <item name="android:windowBackground">@drawable/lists_background</item>
        <item name="android:listViewStyle">@style/listViewPrefs</item>
        <item name="android:checkboxStyle">@style/MyCheckbox</item>

    </style>
    <style name="MyTextAppearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/black</item>
      </style>
    <style name="listViewPrefs" parent="@android:Widget.ListView">
        <item name="android:listSelector">@layout/list_selector_master</item>
        <item name="android:textAppearance">@style/MyTextAppearance</item>
    </style>
    <style name="MyCheckbox" parent="android:Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/btn_check</item>
    </style>

</resources>

清单:

        <activity
            android:name="com.package.SettingsActivity"
            android:theme="@style/PreferencesTheme"
            android:configChanges="keyboard|orientation" >
        </activity>

活动:

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceFragment;

public class SettingsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();

    }
    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);

        }
    }

}

【问题讨论】:

    标签: android android-preferences android-theme


    【解决方案1】:

    这是来自preference.xml 的两个TextView 对象(Preference 的布局):

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />
    
        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />
    

    看来您可以在主题中覆盖 textAppearanceLargetextColorSecondary 以实现颜色更改。这是一个例子:

    <style name="AppTheme">
        <item name="android:textAppearanceLarge">@style/MyTextAppearance</item>
        <item name="android:checkboxStyle">@style/MyCheckBox</item>
    </style>
    
    <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@android:drawable/btn_star</item>
    </style>
    
    <style name="MyTextAppearance" parent="@android:style/TextAppearance.Large">
        <item name="android:textColor">#ff0000</item>
    </style>
    

    【讨论】:

    • 我想就是这样。你能帮我这样做吗,&lt;item name="android:textAppearanceLarge"&gt;@style/MyTextAppearance&lt;/item&gt; 似乎没有成功。
    • @AlexIIP 你把它放在PreferenceTheme 样式中?
    • 是的,我做到了,上面的行来自 PreferenceTheme,你可以看到MyTextAppearancetextColor。我应该将其更改为其他内容吗?
    • 对我来说看起来不错。我不知道为什么它对你不起作用,但对我有用。不知道还有什么建议。对不起:(
    • 我最终不得不重新定义 android:textAppearanceMedium 以更改首选项标题的文本颜色。我对为什么这有效而不是android:textAppearanceLarge 的理解为零。
    【解决方案2】:

    我认为接下来是一个简单明了的方法:

    res/values/styles.xml

    <style name="SettingsFragmentStyle">
    
        <item name="android:textColorSecondary">#222</item>
        <item name="android:textColor">#CCC</item>
        <item name="android:background">#999</item>
        ...
    </style>
    

    在onCreate里面包含PreferenceFragment子类的Activity中:

    setTheme(R.style.SettingsFragmentStyle);
    

    【讨论】:

    • 对我来说,是textColorSecondary(和textColorPrimary)让它发挥了作用。感谢分享!
    【解决方案3】:

    刚刚找到一个可以完成工作的答案。

    此文件是首选项列表项的默认布局:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2006 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
    
              http://www.apache.org/licenses/LICENSE-2.0
    
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <!-- Layout for a Preference in a PreferenceActivity. The
         Preference is able to place a specific widget for its particular
         type in the "widget_frame" layout. -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:paddingRight="?android:attr/scrollbarSize"
        android:background="?android:attr/selectableItemBackground" >
    
        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="6dip"
            android:layout_marginTop="6dip"
            android:layout_marginBottom="6dip"
            android:layout_weight="1">
    
            <TextView android:id="@+android:id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal" />
    
            <TextView android:id="@+android:id/summary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@android:id/title"
                android:layout_alignLeft="@android:id/title"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="?android:attr/textColorSecondary"
                android:maxLines="4" />
    
        </RelativeLayout>
    
        <!-- Preference should place its actual preference widget here. -->
        <LinearLayout android:id="@+android:id/widget_frame"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="vertical" />
    
    </LinearLayout>
    

    您可以将其用作基础并创建自己的自定义布局。此布局必须像这样在每个首选项中应用

    <Preference android:key="somekey" 
    android:title="Title" 
    android:summary="Summary" 
    android:layout="@layout/custom_preference_layout"/>
    

    您可以更改列表之外的整个布局,您只需要一个包含列表视图的布局文件,其中的首选项将像这样填充:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Something" />
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </LinearLayout>
    

    要覆盖 PreferenceFragment 的默认布局,覆盖方法 onCreateView 并更改膨胀视图:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.custom_options_layout, null);
    }
    

    根据这些答案:

    Creating a custom layout for preferences

    How to add a button to PreferenceScreen

    Android: How to adjust Margin/Padding in Preference?

    【讨论】:

    • 由于我无法将自己的答案标记为对赏金的正确答案,因此我仍然接受仅使用主题(似乎是原始发帖人想要的)实现相同的答案。
    【解决方案4】:

    对我来说,上述方法都不起作用。我最终扩展了我使用的那种 Preferences 类,在我的例子中是 CheckBoxPrefernces:

     public class MyPreferenceCheckBox extends CheckBoxPreference {
    
        public MyPreferenceCheckBox(Context context) {
            super(context);
        }
        public MyPreferenceCheckBox(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public MyPreferenceCheckBox(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected View onCreateView(ViewGroup parent) {
            ViewGroup root = (ViewGroup) super.onCreateView(parent);
            ((TextView)root.findViewById(android.R.id.title)).setTextColor(parent.getResources().getColor(R.color.red));
            ((TextView)root.findViewById(android.R.id.summary)).setTextColor(parent.getResources().getColor(R.color.red));
            return root;
        }
    }
    

    以及在prefs.xml中的使用:

     <com.my.package.name.MyPreferenceCheckBox
            android:title="@string/my_title"
            android:defaultValue="true"
            android:key="@string/my_key"
            android:summary="On/Off" />
    

    这个answer 向我展示了那条路径:

    【讨论】:

      【解决方案5】:

      没有足够的代表来评论或投票,但我只是想补充一下 mharper 关于 TextAppearanceMedium 的建议在更改文本颜色方面也对我有用。如果有人知道这是为什么,请解释一下。

      所以只需像这样调整接受的答案:

      <style name="AppTheme">
           <item name="android:textAppearanceMedium">@style/MyTextAppearance</item>
      </style>
      
      <style name="MyTextAppearance" parent="@android:style/TextAppearance.Medium">
           <item name="android:textColor">#ff0000</item>
      </style>
      

      至少如果没有人能解释这是为什么,它可能会阻止有人遇到我在尝试更改文本颜色时遇到的同样困难。

      【讨论】:

      【解决方案6】:

      我可以通过以下方式更改 PreferenceFragment 中的文本颜色:

      styles.xml

      <resources>
      
          <style name="SettingsTheme" parent="android:Theme.Holo">
              <item name="android:textColorPrimary">@color/light_blue_font_color</item>
              <item name="android:textColorSecondary">@color/white_font_color</item>
              <item name="android:textColorTertiary">@color/white_font_color</item>
          </style>
      
      </resources>
      

      colors.xml

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <color name="light_blue_font_color">#33b5e5 </color>
          <color name="white_font_color">#ffffff </color>
      </resources>
      

      AndroidManifest.xml

      <activity
          android:name="xx.yy.zz.SettingsActivity"
          android:theme="@style/SettingsTheme" >
      </activity>
      

      【讨论】:

        【解决方案7】:

        如果您使用的是兼容库,只需添加:

        <item name="colorAccent">#ff0000</item>
        

        你的风格

        【讨论】:

          【解决方案8】:

          我使用 min API 11 并使用 23 进行编译。 设置主题并为此主题设置背景。 如果您使用 > API 14,则可以使用 Theme_DeviceDefault。

          这是我找到的最简单的解决方案:

          public static class SettingsFragment extends PreferenceFragment {
              @Override
              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
          
                  getActivity().setTheme(android.R.style.Theme_Holo);
                  // Load the preferences from an XML resource
                  addPreferencesFromResource(R.xml.settings);
              }
          
              @Override
              public void onViewCreated(View view, Bundle savedInstanceState) {
                  super.onViewCreated(view, savedInstanceState);
                  if(view != null){
                      view.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.background_dark));
                  }
              }
          }
          

          【讨论】:

          • 小心这个,只是重置整个活动的主题。这意味着当您显示另一个片段时,它将具有相同的主题。
          猜你喜欢
          • 1970-01-01
          • 2011-04-29
          • 1970-01-01
          • 1970-01-01
          • 2014-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          相关资源
          最近更新 更多