我想更改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);
}
}
}
答案 0 :(得分:12)
以下是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" />
看起来你可以在主题中覆盖textAppearanceLarge
和textColorSecondary
来实现颜色变化。这是一个例子:
<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>
答案 1 :(得分:6)
我认为下一步是一种简单明了的方法:
RES /值/ 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);
答案 2 :(得分:5)
刚刚找到了完成工作的答案。
此文件是首选项列表项的默认布局:
<?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"/>
您可以更改列表外的整个布局,您只需要一个包含listview的布局文件,其中首选项将填充如下:
<?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
答案 3 :(得分:2)
对我来说,上述方法没有任何作用。我最终扩展了我使用的Prefernces类,在我的案例中CheckBoxPrefernces:
^[a-zA-Z0-9._-]{6,}$
在prefs.xml中使用:
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;
}
}
这answer向我展示了这条道路:
答案 4 :(得分:1)
Haven没有得到足够的评论或评论,但我只是想补充一下,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>
至少如果没有人可以解释为什么会这样,那么可能会阻止某些人尝试更改文字颜色时遇到同样的困难。
答案 5 :(得分:1)
我可以使用以下方法更改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>
答案 6 :(得分:0)
如果您使用的是compat库,只需添加:
<item name="colorAccent">#ff0000</item>
到你的风格
答案 7 :(得分:-1)
我使用min API 11并编译第23章。 设置主题并设置此主题的背景。 如果你使用&gt; 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));
}
}
}