利用AppCompat主题时,SpannableString不工作主题、工作、AppCompat、SpannableString

由网友(汗水比眼泪更漂亮)分享简介:我无法得到SpannableString当我设置AppTheme为Theme.AppCompat.Light.DarkActionBar工作。 我有一个按钮和文本设置有SpannableString。当我使用的Holo主题,如预期的文本渲染,但是当我切换到AppCompat主题跨度效果缝被忽略。我怎样才能获得Spann...

我无法得到SpannableString当我设置AppTheme为Theme.AppCompat.Light.DarkActionBar工作。

我有一个按钮和文本设置有SpannableString。当我使用的Holo主题,如预期的文本渲染,但是当我切换到AppCompat主题跨度效果缝被忽略。我怎样才能获得SpannableString使用AppCompat主题工作?

styles.xml - 那些2主题之间切换时,我得到非常不同的结果......

 <资源>
  <样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar/>
  !<  - <样式名称=AppTheme父=@安卓风格/ Theme.Holo.Light/>  - >
< /资源>
 

......我的按钮,使用SpannableString

 公共静态类PlaceholderFragment扩展片段{

    公共PlaceholderFragment(){
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);

        Button按钮=(按钮)rootView.findViewById(R.id.button);

        字符串细节=ABC;
        字符串标题=的String.Format(2%的,细节);
        Spannable跨度=新SpannableString(字幕);

        INT detailIndex = caption.indexOf(详细);

        span.setSpan(新StyleSpan(Typeface.BOLD),0,detailIndex,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(新RelativeSizeSpan(0.5F),detailIndex,detailIndex + detail.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        button.setText(跨度);

        返回rootView;
    }
}
 

解决方案

那么,它不依赖于 appcompat-V7 。如果删除主题的东西完全消失,而只使用默认的主题,在Android 5.0+你会得到 Theme.Material ,以及同样的效果可以看出那里。

在材料设计美学的部分原因是按钮标题应是全部大写,以他们实施了为消灭你的跨度。 appcompat-V7 适用于在pre-5.0的设备的code,这表明他们的回迁部件的影响并不包括应用程序帽的东西,他们被委托给标准的部件上5.0 +。

添加安卓textAllCaps =假按钮的布局似乎清理的问题。

vivoi主题旧版下载

I'm unable to get SpannableString to work when I set AppTheme to Theme.AppCompat.Light.DarkActionBar.

I have a button and its text is set with a SpannableString. When I use Holo theme the text renders as expected, but when I switch to AppCompat theme the span effects seam to be ignored. How can I get the SpannableString to work using the AppCompat theme?

styles.xml - when switching between those 2 themes I get very different results...

<resources>
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
  <!--<style name="AppTheme" parent="@android:style/Theme.Holo.Light" />-->
</resources>

... for my button that uses 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;
    }
}

解决方案

Well, it's not tied to appcompat-v7. If you remove the theme stuff entirely, and just use the default theme, on Android 5.0+ you will get Theme.Material, and the same effect can be seen there.

Part of the Material Design aesthetic is that button captions should be all caps, and however they implemented that is wiping out your spans. appcompat-v7 works with your code on pre-5.0 devices, suggesting that their backported widget effects do not include the app-caps stuff, and that they are delegating to the standard widgets on 5.0+.

Adding android:textAllCaps="false" to your Button in the layout seems to clear up the problem.

阅读全文

相关推荐

最新文章