小编ram*_*ral的帖子

将Spannable与String.format()结合使用

假设您有以下字符串:

String s = "The cold hand reaches for the %1$s %2$s Ellesse's";
String old = "old"; 
String tan = "tan"; 
String formatted = String.format(s,old,tan); //"The cold hand reaches for the old tan Ellesse's"
Run Code Online (Sandbox Code Playgroud)

假设您希望以此字符串结束,但也Span为任何替换的单词设置了特定的集合String.format.

例如,我们还想做以下事情:

Spannable spannable = new SpannableString(formatted);
spannable.setSpan(new StrikethroughSpan(), oldStart, oldStart+old.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), tanStart, tanStart+tan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

是否有结识的开始指数的稳健的方式oldtan

请注意,只搜索'old'会在'cold'中返回'old',因此无效.

什么的工作,我想,正在寻找%[0-9]$s事前,并计算补偿值以反映在更换String.format.这看起来很令人头疼,我怀疑可能有一种方法String.format可以提供有关其格式化细节的更多信息.好吧,有吗?

android spannablestring spannable

23
推荐指数
3
解决办法
8394
查看次数

在Eclipse中添加库v7 AppCompat时,如何解决错误"找不到与给定名称匹配的资源"?

我有一个API级别10的项目目标,我想实现新的ActionBar支持库.按照支持库设置中的所有说明,将添加到我的项目后,我遇到了许多错误消息,如下所示:

android-support-v7-appcompat\res\values-v14\styles_base.xml:24: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Holo.....

好的,我知道他们是这个问题的很多答案,但是,在尝试了所有更明显的后,我仍然无法解决错误.
我一直在寻找并找到原因,这不是那么明显,至少对像我这样的新手来说.
我的意图是提供一个完整的答案,以帮助所有像我一样的人,并且不掌握Android开发的所有秘密.

eclipse android android-actionbar-compat

14
推荐指数
1
解决办法
4万
查看次数

如何清除可跨文本的格式,包括最后一个字符?

我正在使用此代码从开始到结束删除spannable文本的格式.问题是它正在成功运行,但文本中的最后一个字符仍然是粗体(或斜体/下划线).

removeSpan 没有处理文本中的最后一个字符

            int startSelection = 0;
            int endSelection = text.length();
            if(startSelection>endSelection){
                startSelection  = text.getSelectionEnd();
                endSelection = text.getSelectionStart();
            }

            Spannable str = text.getText();
            StyleSpan[] ss = str.getSpans(startSelection, endSelection, StyleSpan.class);
            for (int i = 0; i < ss.length; i++) {
                if (ss[i].getStyle() == android.graphics.Typeface.BOLD) {
                    str.removeSpan(ss[i]);
                }
                if (ss[i].getStyle() == android.graphics.Typeface.ITALIC) {
                    str.removeSpan(ss[i]);
                }
            }

            UnderlineSpan[] ulSpan = str.getSpans(startSelection, endSelection, UnderlineSpan.class);
            for (int i = 0; i < ulSpan.length; i++) {
                str.removeSpan(ulSpan[i]);
            }

            str.removeSpan(ss[1]);

            text.setText(str);
Run Code Online (Sandbox Code Playgroud)

android

3
推荐指数
3
解决办法
5678
查看次数

android:每行限制10个字符TextView

我从EditText读取值并将其写入TextView

editTitle1.addTextChangedListener(new TextWatcher() {
              public void afterTextChanged(Editable s) {
              }

              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }

              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  s = editTitle1.getText().toString();
                  textTitle1.setText(s);

              }
           });
Run Code Online (Sandbox Code Playgroud)

我希望在一行中 - 最多10个字符,所以如果所有20个字符 - 它是TextView中的两行,每行10个字符.我怎么能这样做?我尝试android:maxLength ="10"但它没有帮助

android textview

3
推荐指数
1
解决办法
4366
查看次数