【问题标题】:Adding language settings button to an app?向应用程序添加语言设置按钮?
【发布时间】:2019-06-14 07:53:49
【问题描述】:

我想在我的应用中添加一个微调器,以更改数组列表中字符串的语言,但不知道该怎么做。

我制作了一个示例应用程序来尝试更改并尝试了几件事,但都没有奏效。我知道这是一件基本的事情,但我真的不知道该怎么做。

这是 XML 布局文件的代码

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Spinner
        android:id="@+id/language_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/list_item"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/list_item"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/list_item"
        android:layout_width="368dp"
        android:layout_height="352dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

主要活动

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Spinner languageSpinner = (Spinner) findViewById(R.id.language_spinner);
        final ListView list = (ListView) findViewById(R.id.list_item);

        /*Creating an ArrayAdapter that is populated with languagelist to populate the languageSpinner*/
        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
                R.array.language_list, android.R.layout.simple_dropdown_item_1line);

        /*Populating the languageSpinner with the spinnerAdapter*/
        languageSpinner.setAdapter(spinnerAdapter);

        languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
                Toast.makeText(getApplicationContext(), "Language changed to " + (CharSequence) languageSpinner.getSelectedItem() , Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView arg0) {
                Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();

            }
        });

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("one");
        arrayList.add("two");
        arrayList.add("three");

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this ,android.R.layout.simple_list_item_1, arrayList ) ;

        list.setAdapter(arrayAdapter);
    }


}

【问题讨论】:

    标签: android-studio settings android-spinner


    【解决方案1】:
                   Resources resources = getResources();
                                            Configuration configuration = resources.getConfiguration();
                                            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
                                            configuration.setLocale(Locale.forLanguageTag("fa"));
                                            resources.updateConfiguration(configuration, displayMetrics);
                                            finish();
                                            startActivity(getIntent());
    

    首先,您必须在资源文件夹的新值文件夹中构建一个新的字符串文件,例如我想在我的应用程序中添加波斯语并构建一个名称为 values-fa 的值文件并复制您的字符串。 xml文件到它 并将字符串更改为您想要的语言。 然后您必须将此代码添加到您的微调器OnItemSelectedListener()。这对我有用。

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 1970-01-01
      • 2010-10-30
      • 2011-11-18
      • 1970-01-01
      • 2020-07-15
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多