我想在应用程序中添加一个微调器,以更改数组列表中字符串的语言,但不确定如何操作。
我制作了一个示例应用程序来尝试进行更改,并尝试了几项措施,但是它们没有用。我知道这是基本的事情,但是我真的不知道该怎么做。
这是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);
}
}
答案 0 :(得分:0)
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
configuration.setLocale(Locale.forLanguageTag("fa"));
resources.updateConfiguration(configuration, displayMetrics);
finish();
startActivity(getIntent());
首先,您必须在资源文件夹的新values文件夹中构建一个新的字符串文件,例如,我想在我的应用程序中添加波斯语,并使用values-fa名称构建一个value文件并复制您的字符串。 xml文件
然后将字符串更改为所需的语言。
然后必须将此代码添加到微调器OnItemSelectedListener()
中。这对我有用。