是资源字符串/字符串连接的串联,可能在布局文件? [英] Is concatenation of resource strings/string concatenation, possible in layout file?

查看:117
本文介绍了是资源字符串/字符串连接的串联,可能在布局文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有资源字符串:

 <字符串名称=地址>地址< /串>
<字符串名称=城市和GT;城市< /串>
<字符串名称=国>国家< /串>
<字符串名称=销code>引脚code< /串>

在我的应用程序,在几个地方,我独自使用这些字符串,并在几个地方,我用的大肠接替他们。结果
我并不怎么创建另一个四个资源字符串:

 <字符串名称=address_with_colon​​>地址:LT; /串>
<字符串名称=city_with_colon​​>城市:其中; /串>
<字符串名称=country_with_colon​​>国家:< /串>
<字符串名称=销code_with_colon​​>引脚code:< /串>

现在,实现这一目标,我要我的串联资源字符串与结肠癌。我知道这是很容易的,虽然java的code,我可以在我的活动类写。但我想是做串联在​​我的布局文件。结果
问题:是字符串连接可能在布局文件结果?
这是我必须执行串联:

 的android:文本=concatenation_if_possible


解决方案

  

问:字符串连接可能的布局文件


作为都能跟得上据我所知。

的一个解决方案是使用什么@DIVA以前回答

另一种可能的解决方案是创建一个扩展的TextView(或要实现这一目标的视图)的自定义视图,并创建一个自定义属性自定义:串联接收字符串参考和自动执行串联。恕我直言,我觉得这是最干净的方法。

在code将如下这样:

 < com.whatever.ConcatenateTextView
    机器人:文字=@字符串/不管
    定制:拼接=@字符串/ second_string/>

或者...您可以使用可绘制的创建自定义TextDrawable(这是在的这个帖子 Github上

复制什么@Devunwired他一下后说:


  

有了这个类,文字,现在是可绘制世界的一部分,这意味着它不仅可以在地方,你通常将图像单独设置,也可以与其它可绘制放入容器,如StateListDrawable或动画与TransitionDrawable和ClipDrawable的喜欢。在很多情况下,我们可以用这个来做,否则将需要多个视图或复合控件只是达到给定的视觉效果工作;因此它可以减少你的视图层次的开销。


这结合您的自定义的TextView正如我前面解释(或任何查看您要使用)为您提供了一个非常强大的选项。再次复制例子@Devunwired在他的文章中写道:

  ImageView的mImageOne;
TextDrawable D =新TextDrawable(本);
d.setText(SAMPLE TEXT \\ n线二);
d.setTextAlign(Layout.Alignment.ALIGN_CENTER);mImageOne.setImageDrawable(四);

如果您需要更多帮助,请让我知道在评论,我会很乐意更新的答案!

I have resource strings :

<string name="address">Address</string>
<string name="city">City</string>
<string name="country">Country</string>
<string name="pincode">Pincode</string>  

In my application, at few places I am using these strings alone and at few places I am succeeding them by a colon.
I don't what to create another four resource strings :

<string name="address_with_colon">Address: </string>
<string name="city_with_colon">City: </string>
<string name="country_with_colon">Country: </string>
<string name="pincode_with_colon">Pincode: </string>  

Now to achieve this, I have to concatenate my resource strings with colon. I know this is very easy though java code which I can write in my activity class. But what I want is to do the concatenation in my layout file.
Question : Is string concatenation possible in layout file?
This is where I have to perform the concatenation:

android:text="concatenation_if_possible"

解决方案

Question : Is string concatenation possible in layout file?

Nope as far as I know.

One solution is to use what @DIVA has answered before.

Another possible solution is to create a custom view that extends TextView (or the view you want to achieve this) and create a custom attribute custom:concatenate which receives a string reference and perform the concatenation automatically. IMHO I think this is the most clean approach.

In code will look as this:

<com.whatever.ConcatenateTextView
    android:text="@string/whatever"
    custom:concatenate="@string/second_string"/>

Or… you can use the power of Drawables creating a custom TextDrawable (which is explained very well by @Devunwired in this post and the concrete implementation of it in Github).

Copying what @Devunwired has said in his post about it:

With this class, text can now be part of the Drawable world, meaning it can not only be set alone in places where you would normally put an image, it can also be placed together with other Drawables in containers like StateListDrawable or animated with the likes of TransitionDrawable and ClipDrawable. In many cases, we can use this to do a job that would otherwise require multiple views or compound controls just to achieve a given visual effect; thus it can reduce overhead in your view hierarchy.

This combined with your custom TextView as I explained before (or whatever view you want to use) gives you a very powerful option. Again copying the example that @Devunwired wrote in his post:

ImageView mImageOne;
TextDrawable d = new TextDrawable(this);
d.setText("SAMPLE TEXT\nLINE TWO");
d.setTextAlign(Layout.Alignment.ALIGN_CENTER);

mImageOne.setImageDrawable(d);

If you need more help please let me know in the comments and I'll gladly update the answer!

这篇关于是资源字符串/字符串连接的串联,可能在布局文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆