Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据android应用程序中的自定义主题在自定义文本视图之间切换_Android_Android Layout_Textview_Android Ui_Android Theme - Fatal编程技术网

根据android应用程序中的自定义主题在自定义文本视图之间切换

根据android应用程序中的自定义主题在自定义文本视图之间切换,android,android-layout,textview,android-ui,android-theme,Android,Android Layout,Textview,Android Ui,Android Theme,在我的应用程序中,我实现了5个不同的自定义应用程序主题 我为这些主题使用了5种不同的字体,我为它们创建了CustomTextview,它扩展了Textview。以下是创建它的格式 public class CustomFontTextView extends TextView { private static final String CUSTOM_FONT = "Custom-Regular.ttf"; private static final String TAG = CustomFontT

在我的应用程序中,我实现了5个不同的自定义应用程序主题

我为这些主题使用了5种不同的字体,我为它们创建了
CustomTextview
,它扩展了
Textview
。以下是创建它的格式

public class CustomFontTextView extends TextView {
private static final String CUSTOM_FONT = "Custom-Regular.ttf";
private static final String TAG = CustomFontTextView.class.getName();

public CustomFontTextView(Context context) {
    super(context);

    if (android.os.Build.VERSION.SDK_INT < 14) {
        setCustomFont(context, CUSTOM_FONT);
    }
}

public CustomFontTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (android.os.Build.VERSION.SDK_INT < 14) {
        setCustomFont(context, CUSTOM_FONT);
    }
}

public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    if (android.os.Build.VERSION.SDK_INT < 14) {
        setCustomFont(context, CUSTOM_FONT);
    }    
}

public boolean setCustomFont(Context ctx, String fontFile) {
    try {
        setTypeface(Typeface.createFromAsset(ctx.getAssets(), fontFile));       
        return true;

    } catch (Exception e) {
        Log.e(TAG, "Could not get typeface: "+e.getMessage());
        return false;
    }
}
公共类CustomFontTextView扩展了TextView{
私有静态最终字符串CUSTOM\u FONT=“CUSTOM Regular.ttf”;
私有静态最终字符串标记=CustomFontTextView.class.getName();
公共CustomFontTextView(上下文){
超级(上下文);
if(android.os.Build.VERSION.SDK_INT<14){
setCustomFont(上下文、自定义字体);
}
}
公共CustomFontTextView(上下文、属性集属性){
超级(上下文,attrs);
if(android.os.Build.VERSION.SDK_INT<14){
setCustomFont(上下文、自定义字体);
}
}
公共CustomFontTextView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
if(android.os.Build.VERSION.SDK_INT<14){
setCustomFont(上下文、自定义字体);
}    
}
公共布尔setCustomFont(上下文ctx,字符串fontFile){
试一试{
setTypeface(Typeface.createFromAsset(ctx.getAssets(),fontFile));
返回true;
}捕获(例外e){
Log.e(标记“无法获取字体:”+e.getMessage());
返回false;
}
}
}

我在XML布局中使用了自定义textview

    <com.myapp.android.fonts.CustomFontTextView
    android:id="@+id/Text_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="@string/my_text_content"
    android:textColor="@android:color/white"
    android:textSize="22sp" />

但是为了实现不同的主题,我必须找到一些方法,根据选择的主题改变我的所有文本视图

有没有办法在我的主题中设置字体

      <style name="MyCustomTheme" parent="android:style/Theme">
               ..............   ......................
             ...........     ...................
         <item name="android:textColorPrimary">#000000</item>
         <item name="android:buttonStyle">@style/MyCustomButton</item>
      </style>

..............   ......................
...........     ...................
#000000
@样式/自定义按钮

我希望在不干扰代码的情况下实现这一点,因为涉及到很多UI组件。

为字体创建自定义属性,并将其包含在样式中。在CustomView类中,访问此属性并相应地设置字体


查看更多详细信息

嗨,Rajesh,我能通过在XML布局中创建TextView而不是customTextview来实现这一点吗?我想使用单一的XML布局在不同的时间显示5种不同的字体(当选择不同的主题时)。我在布局中使用的
com.myapp.android.fonts.CustomFontTextView
不允许我这样做。。!