选择语言android

时间:2015-04-21 12:58:38

标签: android android-fragments

我正在开发一款支持多种语言的应用,您可以从一开始就选择语言。 当应用程序启动时,我想要一个允许您选择语言的片段。每种语言一个按钮。 之后我想打开我的main_Activity,其中有两个片段,但我希望它使用正确的字符串 - ??。xml 我怎么能这样做?

fragment_language

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.user1.lotus.LanguagesFragment"
    android:background="#FAFAFA">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
        android:id = "@+id/languages">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true">

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="0.33">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:id="@+id/english"
                    android:layout_column="0"

                    android:tint="#33B5E5"
                    android:layout_columnWeight="0.50"
                    android:layout_rowWeight="0.33"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:enabled="true"
                    android:elegantTextHeight="false"
                    android:clickable="true"
                    android:text="@string/english"
                    android:singleLine="false" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/french"
                    android:layout_column="1"
                    android:tint="#c8b3ca"
                    android:layout_columnWeight="0.50"
                    android:layout_rowWeight="0.33"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:text="@string/french" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="0.33">

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/greek"
                    android:layout_column="0"


                    android:tint="#b6ff7e"
                    android:layout_columnWeight="0.50"
                    android:layout_rowWeight="0.33"
                    android:layout_gravity="center"
                    android:layout_weight="0.50"
                    android:text="@string/greek" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/spanish"
                    android:layout_column="1"
                    android:tint="#f1c40f"
                    android:layout_columnWeight="0.50"
                    android:layout_rowWeight="0.33"
                    android:layout_gravity="center"
                    android:layout_weight="0.50"
                    android:text="@string/spanish" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="0.33">

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/korean"
                    android:layout_column="0"


                    android:tint="#ff2646"
                    android:layout_columnWeight="0.50"
                    android:layout_rowWeight="0.33"
                    android:layout_gravity="center"
                    android:layout_weight="0.50"
                    android:text="@string/korean" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/arabic"
                    android:layout_column="1"

                    android:tint="#ffa500"
                    android:layout_columnWeight="0.50"
                    android:layout_rowWeight="0.33"
                    android:layout_gravity="center"
                    android:layout_weight="0.50"
                    android:text="@string/arabic" />
            </TableRow>
        </TableLayout>
    </RelativeLayout>


</FrameLayout>

MainActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:id="@+id/voyage">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0px"
        android:layout_weight="7"
        android:layout_height="match_parent">
    </FrameLayout>
    <fragment
        android:id="@+id/side_bar"
        android:layout_width="0px"
        android:layout_weight="3"
        android:layout_height="match_parent"
        class="com.example.user1.lotus.SideBarFragment" >
    </fragment>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

public static void setUserLanguage(final String lng) {
    final Locale locale = new Locale(lng);
    setUserLanguage(locale);
}
public static void setUserLanguage(final Locale locale) {
    Locale.setDefault(locale);
    final Resources res = getAppContext().getResources();
    final Configuration config = res.getConfiguration();
    config.locale = locale;
    //res.updateConfiguration(config, null);
    res.updateConfiguration(config, res.getDisplayMetrics());
}