使用字符串资源在布局xml中以及Java中:

huangapple go评论77阅读模式
英文:

Use String Resource in layout xml along with Java

问题

我在我的layout.xml中有一个按钮视图,如下所示:

<Button
    android:id="@+id/btn_follow"
    android:layout_width="140dp"
    android:layout_height="40dp"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:background="@{viewModel.isMyAccount == 1 ? @drawable/bg_grey_corner_5 : (viewModel.isMyAccount == 2 ? @drawable/bg_gradient : @drawable/bg_strock_corner_5)}"
    android:fontFamily="@font/popins_light"
    android:letterSpacing="0.05"
    android:onClick="@{()->viewModel.setOnItemClick(1)}"
    android:text='@{viewModel.isMyAccount == 1 ? "Unfollow" : (viewModel.isMyAccount == 2 ? "Follow" : "Edit Profile")}'
    android:textAllCaps="false"
    android:textColor="@color/light_white"
    android:textSize="13sp"
    android:textStyle="bold"
    tools:text="Follow">
</Button>

我使用了模型条件来在按钮中显示文本标签,类似如下所示:

android:text='@{viewModel.isMyAccount == 1 ? "Unfollow" : (viewModel.isMyAccount == 2 ? "Follow" : "Edit Profile")}'

但是我正在尝试在我的应用程序中支持多种语言,因此我希望在这里使用字符串资源以及Java模型条件。如果有人知道如何做到这一点,请告诉我,我还是新手,正在学习Android。谢谢!

英文:

I have one button view in my layout.xml like below

&lt;Button
                            android:id=&quot;@+id/btn_follow&quot;
                            android:layout_width=&quot;140dp&quot;
                            android:layout_height=&quot;40dp&quot;
                            android:layout_marginTop=&quot;15dp&quot;
                            android:layout_marginBottom=&quot;15dp&quot;
                            android:background=&quot;@{viewModel.isMyAccount == 1 ? @drawable/bg_grey_corner_5 : (viewModel.isMyAccount == 2 ? @drawable/bg_gradient : @drawable/bg_strock_corner_5)}&quot;
                            android:fontFamily=&quot;@font/popins_light&quot;
                            android:letterSpacing=&quot;0.05&quot;
                            android:onClick=&quot;@{()-&gt;viewModel.setOnItemClick(1)}&quot;
                            android:text=&#39;@{viewModel.isMyAccount == 1 ? &quot;Unfollow&quot; : (viewModel.isMyAccount == 2 ? &quot;Follow&quot; : &quot;Edit Profile&quot;)}&#39;
                            android:textAllCaps=&quot;false&quot;
                            android:textColor=&quot;@color/light_white&quot;
                            android:textSize=&quot;13sp&quot;
                            android:textStyle=&quot;bold&quot;
                            tools:text=&quot;Follow&quot;&gt;

                        &lt;/Button&gt;

It have used model condition for display text label in button like below

android:text=&#39;@{viewModel.isMyAccount == 1 ? &quot;Unfollow&quot; : (viewModel.isMyAccount == 2 ? &quot;Follow&quot; : &quot;Edit Profile&quot;)}&#39;

but I am trying to support multiple language in my app and so I want use string resource there along with java model condition, Let me know if someone have idea about do it, I am new and learning android yet.
Thanks!

答案1

得分: 1

android:text="@{condition ? @string/follow: @string/unfollow}"

英文:

You can use:

android:text=&quot;@{condition ? @string/follow: @string/unfollow}

答案2

得分: 0

代替将关注和取消关注的操作硬编码,可以从字符串资源中检索它们,然后可以为应用程序支持的所有语言添加额外的字符串文件。因此,系统将始终默认为英语,但如果该应用程序在其他国家(例如法国)中使用,并且存在法语资源,那么系统将获取该文件并以法语显示内容。但请记住,您有责任为应用程序中使用的所有字符串添加法语翻译。

英文:

Instead of hard coding the follow and unfollow, retrieve them from string resources using then you can add additional string files for all the languages your app will support. So the system will default to English for example at all times but if the app is being used in other countries for example France and there is Fr resource then the system will grab that file and display the contents in French. But remember it's your responsibility to add French translation for all the strings used in your app.

huangapple
  • 本文由 发表于 2020年7月27日 10:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63107977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定