Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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 如何从reference styleable属性获取RobotoTextView的字体_Android_Styles_Declare Styleable - Fatal编程技术网

Android 如何从reference styleable属性获取RobotoTextView的字体

Android 如何从reference styleable属性获取RobotoTextView的字体,android,styles,declare-styleable,Android,Styles,Declare Styleable,我有一个自定义视图,其中包含几个RobotoTextView,我想为它们设置字体。 因此,我使用如下方式声明可设置样式的项: <declare-styleable name="CustomView"> <attr name="firstTextviewStyle" format="reference" /> <attr name="secondTextviewStyle" format="reference" /> </d

我有一个自定义视图,其中包含几个RobotoTextView,我想为它们设置字体。 因此,我使用如下方式声明可设置样式的项:

<declare-styleable name="CustomView">
        <attr name="firstTextviewStyle" format="reference" />
        <attr name="secondTextviewStyle" format="reference" />
</declare-styleable>

这些属性被引用到样式

<style name="firstTextAppearance">
        <item name="typeface">roboto_medium</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@android:color/white</item>
</style>

机械手中号
16便士
@android:彩色/白色

所以,问题是,如何从中获取字体属性值?

@nullpex对于应用程序中的动态或运行时字体更改,您可以这样做,如以下示例所示。您也可以在布局文件中使用它,因为有自定义视图也具有自定义属性

我已经在GitHub上为以下代码创建了一个存储库

请随便问任何问题

CustomTextViewActivity.java

package com.harsh.customtextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class CustomTextViewActivity extends AppCompatActivity implements View.OnClickListener {

    CustomTextView customTextView1, customTextView2;
    Button changeToRoboto, changeToCalculus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);

        customTextView1 = (CustomTextView) findViewById(R.id.customTextView1);
        customTextView2 = (CustomTextView) findViewById(R.id.customTextView2);

        changeToRoboto = (Button) findViewById(R.id.change_to_roboto);
        changeToCalculus = (Button) findViewById(R.id.change_to_calculus);
        changeToRoboto.setOnClickListener(this);
        changeToCalculus.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_to_roboto:
                customTextView1.setFont(getString(R.string.font_roboto_regular));
                customTextView2.setFont(getString(R.string.font_roboto_regular));
                break;
            case R.id.change_to_calculus:
                customTextView1.setFont(getString(R.string.font_calculus_sans));
                customTextView2.setFont(getString(R.string.font_calculus_sans));
                break;
        }
    }
}
package com.harsh.customtextview;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;

@SuppressLint("AppCompatCustomView")
public class CustomTextView extends TextView {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CustomTextView(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomView);
            String fontName = a.getString(R.styleable.CustomView_fonts);

            try {
                if (fontName != null) {
                    Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
                    setTypeface(myTypeface);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            a.recycle();
        }
    }

    public void setFont(String fontName) {
        setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName));
    }
}
activity\u text\u view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.harsh.customtextview.CustomTextViewActivity">

    <com.harsh.customtextview.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/themed_text"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:theme="@style/roboto_theme"/>

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/calculus_sans"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:fonts="@string/font_calculus_sans" />

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/roboto_regular"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        app:fonts="@string/font_roboto_regular" />

    <Button
        android:id="@+id/change_to_roboto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Roboto" />

    <Button
        android:id="@+id/change_to_calculus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Calculus" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="fonts" format="string" />
    </declare-styleable>
</resources>
<resources>
    <string name="app_name">CustomTextView</string>
    <string name="font_calculus_sans">Calculus_Sans.ttf</string>
    <string name="font_roboto_regular">Roboto_Regular.ttf</string>
    <string name="calculus_sans">Calculus Sans</string>
    <string name="roboto_regular">Roboto Regular</string>
    <string name="themed_text">Themed Text</string>
</resources>
attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.harsh.customtextview.CustomTextViewActivity">

    <com.harsh.customtextview.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/themed_text"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:theme="@style/roboto_theme"/>

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/calculus_sans"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:fonts="@string/font_calculus_sans" />

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/roboto_regular"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        app:fonts="@string/font_roboto_regular" />

    <Button
        android:id="@+id/change_to_roboto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Roboto" />

    <Button
        android:id="@+id/change_to_calculus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Calculus" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="fonts" format="string" />
    </declare-styleable>
</resources>
<resources>
    <string name="app_name">CustomTextView</string>
    <string name="font_calculus_sans">Calculus_Sans.ttf</string>
    <string name="font_roboto_regular">Roboto_Regular.ttf</string>
    <string name="calculus_sans">Calculus Sans</string>
    <string name="roboto_regular">Roboto Regular</string>
    <string name="themed_text">Themed Text</string>
</resources>

styles.xml


@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@字符串/字体\u演算\u sans

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.harsh.customtextview.CustomTextViewActivity">

    <com.harsh.customtextview.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/themed_text"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:theme="@style/roboto_theme"/>

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/calculus_sans"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:fonts="@string/font_calculus_sans" />

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/roboto_regular"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        app:fonts="@string/font_roboto_regular" />

    <Button
        android:id="@+id/change_to_roboto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Roboto" />

    <Button
        android:id="@+id/change_to_calculus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Calculus" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="fonts" format="string" />
    </declare-styleable>
</resources>
<resources>
    <string name="app_name">CustomTextView</string>
    <string name="font_calculus_sans">Calculus_Sans.ttf</string>
    <string name="font_roboto_regular">Roboto_Regular.ttf</string>
    <string name="calculus_sans">Calculus Sans</string>
    <string name="roboto_regular">Roboto Regular</string>
    <string name="themed_text">Themed Text</string>
</resources>

自定义文本视图
微积分
Roboto_Regular.ttf
微积分
机器人正规
主题文本

@nullpex对于应用程序中的动态或运行时字体更改,您可以执行类似操作,如以下示例所示。您也可以在布局文件中使用它,因为有自定义视图也具有自定义属性

我已经在GitHub上为以下代码创建了一个存储库

请随便问任何问题

CustomTextViewActivity.java

package com.harsh.customtextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class CustomTextViewActivity extends AppCompatActivity implements View.OnClickListener {

    CustomTextView customTextView1, customTextView2;
    Button changeToRoboto, changeToCalculus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);

        customTextView1 = (CustomTextView) findViewById(R.id.customTextView1);
        customTextView2 = (CustomTextView) findViewById(R.id.customTextView2);

        changeToRoboto = (Button) findViewById(R.id.change_to_roboto);
        changeToCalculus = (Button) findViewById(R.id.change_to_calculus);
        changeToRoboto.setOnClickListener(this);
        changeToCalculus.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_to_roboto:
                customTextView1.setFont(getString(R.string.font_roboto_regular));
                customTextView2.setFont(getString(R.string.font_roboto_regular));
                break;
            case R.id.change_to_calculus:
                customTextView1.setFont(getString(R.string.font_calculus_sans));
                customTextView2.setFont(getString(R.string.font_calculus_sans));
                break;
        }
    }
}
package com.harsh.customtextview;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;

@SuppressLint("AppCompatCustomView")
public class CustomTextView extends TextView {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CustomTextView(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomView);
            String fontName = a.getString(R.styleable.CustomView_fonts);

            try {
                if (fontName != null) {
                    Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
                    setTypeface(myTypeface);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            a.recycle();
        }
    }

    public void setFont(String fontName) {
        setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName));
    }
}
activity\u text\u view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.harsh.customtextview.CustomTextViewActivity">

    <com.harsh.customtextview.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/themed_text"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:theme="@style/roboto_theme"/>

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/calculus_sans"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:fonts="@string/font_calculus_sans" />

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/roboto_regular"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        app:fonts="@string/font_roboto_regular" />

    <Button
        android:id="@+id/change_to_roboto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Roboto" />

    <Button
        android:id="@+id/change_to_calculus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Calculus" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="fonts" format="string" />
    </declare-styleable>
</resources>
<resources>
    <string name="app_name">CustomTextView</string>
    <string name="font_calculus_sans">Calculus_Sans.ttf</string>
    <string name="font_roboto_regular">Roboto_Regular.ttf</string>
    <string name="calculus_sans">Calculus Sans</string>
    <string name="roboto_regular">Roboto Regular</string>
    <string name="themed_text">Themed Text</string>
</resources>
attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.harsh.customtextview.CustomTextViewActivity">

    <com.harsh.customtextview.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/themed_text"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:theme="@style/roboto_theme"/>

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/calculus_sans"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:fonts="@string/font_calculus_sans" />

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/roboto_regular"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        app:fonts="@string/font_roboto_regular" />

    <Button
        android:id="@+id/change_to_roboto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Roboto" />

    <Button
        android:id="@+id/change_to_calculus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Calculus" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="fonts" format="string" />
    </declare-styleable>
</resources>
<resources>
    <string name="app_name">CustomTextView</string>
    <string name="font_calculus_sans">Calculus_Sans.ttf</string>
    <string name="font_roboto_regular">Roboto_Regular.ttf</string>
    <string name="calculus_sans">Calculus Sans</string>
    <string name="roboto_regular">Roboto Regular</string>
    <string name="themed_text">Themed Text</string>
</resources>

styles.xml


@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@字符串/字体\u演算\u sans

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.harsh.customtextview.CustomTextViewActivity">

    <com.harsh.customtextview.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/themed_text"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:theme="@style/roboto_theme"/>

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/calculus_sans"
        android:textColor="@color/colorPrimary"
        android:textSize="22sp"
        app:fonts="@string/font_calculus_sans" />

    <com.harsh.customtextview.CustomTextView
        android:id="@+id/customTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/roboto_regular"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        app:fonts="@string/font_roboto_regular" />

    <Button
        android:id="@+id/change_to_roboto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Roboto" />

    <Button
        android:id="@+id/change_to_calculus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Change to Calculus" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="fonts" format="string" />
    </declare-styleable>
</resources>
<resources>
    <string name="app_name">CustomTextView</string>
    <string name="font_calculus_sans">Calculus_Sans.ttf</string>
    <string name="font_roboto_regular">Roboto_Regular.ttf</string>
    <string name="calculus_sans">Calculus Sans</string>
    <string name="roboto_regular">Roboto Regular</string>
    <string name="themed_text">Themed Text</string>
</resources>

自定义文本视图
微积分
Roboto_Regular.ttf
微积分
机器人正规
主题文本

无论您描述的是什么,都不可能需要更多的解释。@nullpex您希望通过这样做来实现什么可能有一个简单的解决方案way@HarshSharma,下一个目标是:在打开CustomView的同时设置RobotoTextView样式。我使用TypedArray来获取此信息,但我无法从style中获取字体属性。不可能您描述的任何内容都需要更多解释。@nullpex您希望通过这样做实现什么可能有一个简单的way@HarshSharma,下一个目标是:在打开CustomView的同时设置RobotoTextView样式。我使用TypedArray获取此信息,但无法从布局文件中的style.font获取字体属性,预定义的字体属性正在塌陷,当我在属性文件中将其更改为fontName时,获取另一个错误:找不到符号变量CustomView\u font。请respond@GaneshChowdharySadanala,如果在attr文件中将其更改为“fontName”,则必须在布局文件中查找“CustomView_fontName”而不是“CustomView_font”字体,并且预定义的字体属性正在塌陷,当我在attr文件中将其更改为fontName时,会出现另一个错误:找不到符号变量CustomView_font。请respond@GaneshChowdharySadanala,如果在attr文件中将其更改为“fontName”,则必须查找“CustomView\u fontName”而不是“CustomView\u font”