当我将AppTheme设置为Theme.AppCompat.Light.DarkActionBar时,我无法使SpannableString工作.
我有一个按钮,其文本设置为SpannableString.当我使用Holo主题时,文本按预期呈现,但是当我切换到AppCompat主题时,span效果会被忽略.如何使用AppCompat主题使SpannableString工作?
styles.xml - 当在这两个主题之间切换时,我得到了非常不同的结果......
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
<!--<style name="AppTheme" parent="@android:style/Theme.Holo.Light" />-->
</resources>
Run Code Online (Sandbox Code Playgroud)
...对于使用SpannableString的按钮
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
String detail = "ABC";
String caption = String.format("2 %s", detail);
Spannable span = new SpannableString(caption);
int detailIndex = caption.indexOf(detail);
span.setSpan(new StyleSpan(Typeface.BOLD), 0, detailIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new RelativeSizeSpan(0.5f), detailIndex, detailIndex+detail.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(span);
return rootView; …
Run Code Online (Sandbox Code Playgroud)