英文:
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
<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
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.
Thanks!
答案1
得分: 1
android:text="@{condition ? @string/follow: @string/unfollow}"
英文:
You can use:
android:text="@{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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论