2015-04-21 2 views
0

Я делаю приложение, которое поддерживает несколько языков, на которых вы выбираете свой язык в начале. Когда приложение запускается, я хотел бы иметь фрагмент, который позволяет вам выбрать язык. Одна кнопка для каждого языка. После этого я хотел бы открыть мою main_Activity, в которой есть два фрагмента, но я бы хотел, чтобы она использовала правильную строку - ??. Xml Как я могу это сделать?Выберите язык android

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> 
+0

Для этого вам нужно сделать различные 'папку values'. Предположим, что если вы хотите реализовать приложение для 'English' и' Arabic', то папки будут 'values-en' и' values-ar'. И ваши файлы 'strings.xml' имеют одинаковое имя строки – Piyush

+0

Возможный дубликат [Как создать многоязычное приложение для Android?] (Http://stackoverflow.com/questions/4259128/how-can-i-create- a-multilingual-android-application) –

ответ

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()); 
} 
+0

Не означает ли это, что язык устройства? Я хочу выбрать с помощью кнопок ... – Pet

+0

Просто передайте соответствующую 'языку' или строку языка ISO, такую ​​как' en' ot 'it', и она переключит ваше приложение на использование соответствующего' strings.xml'. – Stan

Смежные вопросы