gpt4 book ai didi

java - 切换主题时如何考虑变化?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:09:53 25 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,它需要根据服务器提供的 themeCode 切换主题。我正在使用 sharePref 保存主题代码并使用 setTheme(R.style.themeName); 应用它。它工作正常,直到像

这样的基本主题属性
colorPrimary
colorPrimaryDark
colorAccent
windowActionBar
windowNoTitle

为此,我在 styles.xml 中创建了不同的样式。但我有一个限制,有些字段说 EditText 有变化 EditText

  • 人名
  • 电子邮件
  • 电话
  • 密码等

同样,TextView 也有 TextView 的变化

  • 标题
  • 单行
  • 多线
  • 链接等

在多主题要求之前,我已经为所有人创建了单独的主题

  • Apptheme.Edittext.email
  • Apptheme.Edittext.Password
  • Apptheme.Edittext.PersonName 等并应用于 xml 中的特定 View ,如

     style="@style/AppTheme.EditText.PersonName"  

现在我看了很多教程/帖子,但没有找到解决属性变化的方法。请帮助应用这些变化,对此我将不胜感激。

问候:因齐玛姆·塔里克

最佳答案

在我看来,在运行时更改应用主题,肯定需要重新加载 Activity;这在大多数情况下会在某些时候产生问题(如果项目扩展到中等规模,具有用户控制,如切换或开关,如果用户重复点击切换应用可能很容易崩溃)


我建议使用自定义控件类( TextView 、按钮等);其中此属性与来自 sharedPref 的当前主题值不同。这种方法有一个缺点;它将需要手动更改当前屏幕的所有 View 和内存中已经呈现的 View (如果有的话),其余的与我们的传统方法相比,它将更加平滑地过渡

编辑:CustomTextView 的例子##

这是自定义 TextView 类的示例

public class CustomTextView extends android.support.v7.widget.AppCompatTextView {
private static final String TAG = "TextView";
private Typeface tf = null;
private SharedPreferenceUtils preferenceUtils = SharedPreferenceUtils.getInstance();

/**
* @param context:This is an abstract class whose implementation is provided by Android Operating System.
* @param attrs:A collection of attributes, as found associated with a tag in an XML document.
* @param defStyle:
*/
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (preferenceUtils.getBooleanValue(PrefsKeyValue.bTheme)) {
this.setTextColor(ResourceUtils.getColor(R.color.lightThemeTextColor));
} else {
this.setTextColor(ResourceUtils.getColor(R.color.colorWhite));
}

try {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CustomEditText, defStyle, 0);

String str = a.getString(R.styleable.CustomTextView_FontEnum);
int original = a.getInt(R.styleable.CustomEditText_FontEnum, 0);
CustomEnum.CustomFontType customEnumValue = CustomEnum.CustomFontType.fromId(a.getInt(R.styleable.CustomEditText_FontEnum, 0));
a.recycle();
switch (customEnumValue) {
case BOLD:
setTypeface(HelveticaNeueBold.getInstance(context).getTypeFace());
break;

case LIGHT:
setTypeface(HelveticaNeueMedium.getInstance(context).getTypeFace());
break;

case REGULAR:
setTypeface(HelveticaNeue.getInstance(context).getTypeFace());
break;

default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}

public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public boolean setCustomFont(Context ctx, String asset) {

try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
LogUtils.LogE(TAG, "Could not get typeface: " + e.getMessage());
return false;
}

setTypeface(tf);
return true;
}}

这里我根据 sharedPref 的主题值更改了文本颜色

 if (preferenceUtils.getBooleanValue(PrefsKeyValue.bTheme)) {
this.setTextColor(ResourceUtils.getColor(R.color.lightThemeTextColor));
} else {
this.setTextColor(ResourceUtils.getColor(R.color.colorWhite));
}

然后在xml文件中使用这个类作为textview标签。

    <com.mypkg.customview.CustomTextView
style="@style/signup_textViewStyle"
android:text="@string/activity_login_password" />

我相信,您可以用相同的方式处理控件主题的属性变化。

关于java - 切换主题时如何考虑变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48781864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com