2016-12-06 3 views
5

私はこの単純なコードでスパン可能な文字列を設定するのがなぜこのテキストビューで機能しないのか理解できません。以下のメソッドは、日付が現在の日付である場合、日付を表示するテキストの前に緑色で表示される "Today"マーカーを追加します。スパニング可能な文字列を単純なTextviewで使用できないように設定する

private void setTimeTextView(String timeString) { 

    Calendar c = Calendar.getInstance(); 

    String todaysDateString = ApiContentFormattingUtil.getFullDateFormat(c.getTime()); 
    if (timeString.equals(todaysDateString)){ 
     String todayText = getResources().getString(R.string.today_marker); 

     Spannable timeSpannable = new SpannableString(todayText + timeString); 
     timeSpannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.greenish_teal)), 0, 
       todayText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
     mDateTime.setText(timeSpannable); 
    } else { 
     mDateTime.setText(timeString); 
    } 
} 

ただし、色は変更されません。ここで

enter image description here

textAllCaps属性は、あなたのString上の任意のSpannableの情報をこのビューのXML

<TextView 
     android:id="@+id/newsfeed_date_time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="23dp" 
     android:textSize="12sp" 
     android:textColor="@color/white_three" 
     android:letterSpacing="0.06" 
     app:fontPath="@string/opensans_bold_path" 
     tools:text="Monday, January 1st" 
     android:textAllCaps="true" 
     tools:ignore="MissingPrefix" 
     tools:targetApi="lollipop"/> 
+0

注: 'アンドロイド:= "true" を'、それが働いたことを削除した後、あなたに感謝そんなにtextAllCaps:textAllCaps = "true" の '私は'アンドロイドを使用していた –

答えて

21

ストリッピングされています。それを削除(またはfalseに設定)し、SpannableStringを作成する前に自分で大文字に変換する必要があります。たとえば:

String todayText = getResources().getString(R.string.today_marker); 
String text = todayText + timeString; 

Spannable timeSpannable = new SpannableString(text.toUpperCase()); 

これは、具体的AllCapsTransformationMethodで、textAllCaps属性を持つ既知のバグです。

http://code.google.com/p/android/issues/detail?id=67509

+1

をSpannableString解除されます! –

関連する問題