简体   繁体   English

在布局 xml 和 Java 中使用字符串资源

[英]Use String Resource in layout xml along with Java

I have one button view in my layout.xml like below我的布局中有一个按钮视图。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>

It have used model condition for display text label in button like below它已使用 model 条件在按钮中显示文本 label,如下所示

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

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.但我试图在我的应用程序中支持多种语言,所以我想在那里使用字符串资源以及 java model 条件,让我知道是否有人知道这样做,我是新手,还在学习 ZC31B32364CE19CA58FCD150A417。 Thanks!谢谢!

You can use:您可以使用:

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

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.因此,系统将始终默认为英语,但如果该应用程序正在其他国家(例如法国)使用并且有 Fr 资源,则系统将抓取该文件并以法语显示内容。 But remember it's your responsibility to add French translation for all the strings used in your app.但请记住,为应用程序中使用的所有字符串添加法语翻译是您的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM