将布局xml中的字符串资源与Java一起使用

将布局xml中的字符串资源与Java一起使用,java,android,android-layout,Java,Android,Android Layout,我在layout.xml中有一个按钮视图,如下所示 <Button android:id="@+id/btn_follow" android:layout_width="140dp" android:layout_height="40dp"

我在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。 谢谢

您可以使用:

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

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

因此它将类似于@{viewModel.isMyAccount==1?context.getString(R.string.follow)?是的,如果不起作用,请尝试以下android:text=“@{condition?@string/follow:@string/unfollow}”@string/follow工作正常!编辑您的答案,让我接受您的回答您是说我应该使用片段而不是xml显示文本?