私はアンドロイドの発展の世界では新しいです、そして、時間の各桁がそれ自身の書体を持っているような時計を作りたいと思います。 時間桁には独自の書体があり、分桁には独自の書体があります。 どうすればいいですか?助けて。時計のテキストのカスタムフォント
答えて
あなたのフォント名は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" />
私は2つの異なるフォントを使いたいです。 1つは数時間、もう1つは数分です。 mainActivity.javaの完全な内容をactivity_mainレイアウトで送ってくれますか?これまでに試したことを –
に教えてください。私たちはあなたのコードを実行するためではありません。 –
2つの異なるフォントのために2つの異なるクラスを作り、あなたが望むようにそれを使用してください。 –
は、あなたが通常.OTF形式のあるフォントファイルを、ダウンロードする必要があります。次に、このフォントをAndroidスタジオまたは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に毎回の書体を設定するよりも、書体が適用されるように、独自のカスタムの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>
それぞれに2つの異なるフォントを使用したいと思います。 1つは数時間、もう1つは数分です。 –
Ok ..何時間も別のテキストビューを作成し、別のタイプの顔を設定する –
しかし、それを現在の時刻に関連付ける方法。現在時刻を表示します –
まず、フォントをプロジェクトのassetsフォルダにコピーします。
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ファイル内秒間分のTextView
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);
}
}
、1時間のTextView
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);
}
}
については
TextViewには、これを行う<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>
提供したコードは静的テキストです。私は時計アプリを作っているので、時間に添付したい。現在の時刻を表示するようにします。 –
あなたのテキストビューを更新したいですか? –
しかし、どのような価値を置くべきですか。現時点で自動的に変更する必要があります –
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);
}
}
- 1. カスタムフォント+テキストフィールドプレースホルダーとテキスト
- 2. カスタムフォントとテキストのアウトライン/ストロークを持つUIButton
- 3. 集計時間分秒テキストの列 - Excel
- 4. カスタムフォントのcenterではなく、viewDidLoadの左揃えのテキスト
- 5. のFirefox 56は、このスクリーンショットの時に任意のカスタムフォント
- 6. iOS 10と属性付きテキストのカスタムフォントを設定する
- 7. FB Litho:テキストのカスタムフォントを設定する方法
- 8. テキストをCのカスタムフォントでイメージに変換する方法#
- 9. カスタムフォント
- 10. オペラモバイルエミュレータのカスタムフォント
- 11. HTMLのCSSカスタムフォント
- 12. android projectのカスタムフォント
- 13. ckeditorのカスタムフォント
- 14. phonegapプロジェクトのカスタムフォント
- 15. iOSのカスタムフォント
- 16. カスタムフォントのインストール
- 17. JS UWPのカスタムフォント
- 18. Tabbarのカスタムフォント
- 19. ActionBarSherlockタブのカスタムフォント
- 20. カスタムフォントの追加
- 21. カスタムフォントのKivyラベルマークアップ
- 22. EmojiCompatのカスタムフォント
- 23. customAdapterのカスタムフォント
- 24. Wordpressのカスタムフォントが
- 25. ListViewのカスタムフォント
- 26. Pixi.jsのカスタムフォント
- 27. XCodeのカスタムフォント4.3.2
- 28. Android Widgetのカスタムフォント
- 29. SAPUI5テーマデザイナのカスタムフォント
- 30. Visualforceページのカスタムフォント
カスタムフォントを使用して別の色を設定したいのですか? –
私はカスタムフォントが欲しいです。助けてください –