【问题标题】:Custom font for clock textview时钟文本视图的自定义字体
【发布时间】:2016-08-22 14:46:24
【问题描述】:

我是 android 开发领域的新手,我想制作一个时钟,让每个时间数字都有自己的字体。 小时数字有自己的字体,分钟数字有自己的字体。 我怎样才能做到这一点。帮帮我。

【问题讨论】:

  • 具体到您的问题,您想使用自定义字体并设置不同的颜色?
  • 我想要自定义字体。请帮帮我
  • 你试过 android Chronometer 来做这个吗?
  • 很多“帮帮我”,没有任何技术细节,没有任何研究的迹象,没有公认的答案,没有明显的支持。 -1。如果您是某个世界的新手,请从研究它开始,这是您在开发某些东西时最常做的事情......

标签: java android textview simpledateformat


【解决方案1】:
public class MainActivity extends AppCompatActivity {

public TextView textView;
int countInt;
private int mInterval = 1000; // 1 second by default, can be changed later
private Handler mHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=(TextView)findViewById(R.id.textView);

    mHandler = new Handler();
    startRepeatingTask();
}

Runnable mStatusChecker = new Runnable() {
    @Override
    public void run() {
        try {
            countInt=countInt+1;
            textView.setText(String.valueOf(countInt));
        } finally {
            mHandler.postDelayed(mStatusChecker, mInterval);
        }
    }
};

void startRepeatingTask() {
    mStatusChecker.run();
}

void stopRepeatingTask() {
    mHandler.removeCallbacks(mStatusChecker);
}
}

【讨论】:

    【解决方案2】:

    首先将字体复制到项目中的 assets 文件夹中。

    一小时文本视图

    public class HourTextView extends TextView {
    
    public HourTextView(Context context) {
        super(context);
        init(null);
    }
    
    public HourTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }
    
    public HourTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
    
    public HourTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
    
    // Initializes any UI properties of the text view.
    private void init(AttributeSet attrs) {
        Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "Hour-font-file.otf");
        setTypeface(myTypeface);
    }
    
    }
    

    分钟文本视图

    public class MinuteTextView extends TextView {
    
    public MinuteTextView(Context context) {
        super(context);
        init(null);
    }
    
    public MinuteTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }
    
    public MinuteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
    
    public MinuteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
    
    // Initializes any UI properties of the text view.
    private void init(AttributeSet attrs) {
        Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "Minute-font-file.otf");
        setTypeface(myTypeface);
    }
    
    }
    

    秒文本视图

    public class SecondTextView extends TextView {
    
    public SecondTextView(Context context) {
        super(context);
        init(null);
    }
    
    public SecondTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }
    
    public SecondTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
    
    public SecondTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
    
    // Initializes any UI properties of the text view.
    private void init(AttributeSet attrs) {
        Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "Second-font-file.otf");
        setTypeface(myTypeface);
    }
    
    }
    

    并在 xml 文件中执行此操作,

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    
    <com.yourpackage.HourTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="10"
        android:id="@+id/hourText" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=" : " />
    
    <com.yourpackage.MinuteTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="45 "
        android:id="@+id/minuteText" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=" : " />
    
    <com.yourpackage.SecondTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="28"
        android:id="@+id/secondsText" />
    
    </LinearLayout>
    

    【讨论】:

    • 您提供的代码是静态文本。我想在制作时钟应用程序时将其附加到时间。所以让它显示当前时间。
    • 但是要设置什么值。它应该随着当前时间自动变化
    • 你应该使用定时器任务来做到这一点。
    • 怎么做。你能解释一下吗?
    【解决方案3】:

    首先,您需要下载一个字体文件,该文件通常为 .otf 格式。然后,您需要将此字体导入到您的 android studio 或 eclipse 项目中的 assets 文件夹中。完成此操作后,您可以创建一个新字体并将其设置为您的文本视图。就小时和分钟数字使用不同的字体而言,您需要创建具有多个文本视图的布局。例如,您可以执行以下操作

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hours_digit"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=": "
        android:id="@+id/time_colon"
        android:layout_toEndOf="@id/hours_digit" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/time_colon"
        android:id="@+id/minutes_digit"/>
    
    </RelativeLayout>
    

    实现此目的的另一种方法是创建自己的自定义文本视图,而不是每次都将字体设置为 textview,以便在您使用时应用字体。例如,对于分钟文本视图,您可以这样做:

    public class MinutesTextView extends TextView {
    
    // Constructor method for the text view...
    public MinutesTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }
    
    // Constructor method for the text view...
    public MinutesTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    
    }
    
    // Constructor method for the text view...
    public MinutesTextView(Context context) {
        super(context);
        init(null);
    }
    
    // Initializes any UI properties of the text view.
    private void init(AttributeSet attrs) {
        Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "Minutes-font-file.otf");
        setTypeface(myTypeface);
    }
    

    }

    以及,使用之前的布局文件。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <com.example.yourpackage.MinutesTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hours_digit"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=": "
        android:id="@+id/time_colon"
        android:layout_toEndOf="@id/hours_digit" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/time_colon"
        android:id="@+id/minutes_digit"/>
    
    </RelativeLayout>
    

    【讨论】:

    • 我想为每个使用两种不同的字体。一小时一分钟。
    • 好的.. 再制作几个小时的文本视图并为其设置不同的字体
    • 但是如何将它与当前时间联系起来。所以它显示当前时间
    【解决方案4】:

    假设您的字体名称是 DemoFont 。创建一个扩展TextView 的类。并初始化 DemoFont 的字体。

    接下来将该字体的.ttf 文件放在assets 文件夹中。

    public class DemoFont extends TextView {
    
    
        public DemoFont (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        public DemoFont (Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public DemoFont (Context context) {
            super(context);
            init();
        }
    
        private void init() {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                    "demofont.ttf");
            setTypeface(tf);
        }
    
    }
    

    现在,在您的布局文件中,您可以像这样使用它。

    <YOUR_PACKAGE_NAME.DemoFont
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    【讨论】:

    • 我想使用两种不同的字体。一小时一分钟。你能把带有activity_main布局的MainActivity.java的完整内容发给我吗?请帮忙
    • 分享你到目前为止所尝试的。我们不是来做你的代码的。
    • 对于两种不同的字体,制作 2 个不同的类,并根据需要使用它。
    • 但是如何让它显示当前时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 2012-10-16
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多