如果语言,Android View GONE

时间:2014-09-12 14:53:53

标签: android layout view

如果设备设置为英国联合王国,我不想显示布局。最好的方法是什么?每次获取设备语言?

4 个答案:

答案 0 :(得分:1)

这样,GB代表英国(英国)

String locale = context.getResources().getConfiguration().locale.getCountry(); 

if(locale.equals("GB")) {
//view.setVisibility(View.GONE);
//British people are cooool ;)
}else {
//do whatever you want to do
}

答案 1 :(得分:1)

setVisibility的解决方案有效,但OP希望知道最佳方式是什么,以避免每次都获取运行时语言。

因此:

为您想要的语言创建单独的布局(XML文件)。

例如,创建文件夹layout-en-rGB并在此文件夹中复制要修改的XML文件,然后进行所需的修改。

在运行时,每次语言为英语 - 英语时,都会自动加载此布局。

答案 2 :(得分:0)

每次使用以下代码检查设备的语言:

Locale.getDefault().getDisplayLanguage();

如果没有,请更改视图的可见性,如下所示:

view.setVisibility(View.GONE);

答案 3 :(得分:0)

通常最好的方法是为视图指定样式,然后创建该样式的默认实现并覆盖en-rGB语言环境的样式。如果你的布局不是那么简单,那么这是最好的方法,因为你不想覆盖整个布局文件只是为了覆盖一个属性。

1)为要在布局文件中隐藏的视图指定样式。我在这里使用TextView:

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/TastyFoodEditText" />

2)在res / values / styles.xml中定义样式

<resources>
    <style name="TastyFoodEditText" parent="android:style/Widget.EditText">
    </style>
</resources>

3)通过使用此内容创建文件res / values-en-rGB / styles.xml来覆盖英国语言环境的样式

<resources>
    <style name="TastyFoodEditText" parent="android:style/Widget.EditText">
        <item name="android:visibility">gone</item>
    </style>
</resources>
相关问题