2016-04-28 3 views
0

私はアンドロイドの発展の世界では新しいです、そして、時間の各桁がそれ自身の書体を持っているような時計を作りたいと思います。 時間桁には独自の書体があり、分桁には独自の書体があります。 どうすればいいですか?助けて。時計のテキストのカスタムフォント

+0

カスタムフォントを使用して別の色を設定したいのですか? –

+0

私はカスタムフォントが欲しいです。助けてください –

答えて

0

あなたのフォント名は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" /> 
+0

私は2つの異なるフォントを使いたいです。 1つは数時間、もう1つは数分です。 mainActivity.javaの完全な内容をactivity_mainレイアウトで送ってくれますか?これまでに試したことを –

+0

に教えてください。私たちはあなたのコードを実行するためではありません。 –

+0

2つの異なるフォントのために2つの異なるクラスを作り、あなたが望むようにそれを使用してください。 –

0

は、あなたが通常.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> 
+0

それぞれに2つの異なるフォントを使用したいと思います。 1つは数時間、もう1つは数分です。 –

+0

Ok ..何時間も別のテキストビューを作成し、別のタイプの顔を設定する –

+0

しかし、それを現在の時刻に関連付ける方法。現在時刻を表示します –

0

まず、フォントをプロジェクトの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> 
+0

提供したコードは静的テキストです。私は時計アプリを作っているので、時間に添付したい。現在の時刻を表示するようにします。 –

+0

あなたのテキストビューを更新したいですか? –

+0

しかし、どのような価値を置くべきですか。現時点で自動的に変更する必要があります –

0
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); 
} 
} 
関連する問題