Android 斯宾纳赢得';不显示字符串条目
我的程序应该使用微调器来显示颜色名称列表,并具有其背景色。例如,在显示的图像中,红色应该显示一个黑色文本,表示“红色”以指定它。 我有Android 斯宾纳赢得';不显示字符串条目,android,android-studio,Android,Android Studio,我的程序应该使用微调器来显示颜色名称列表,并具有其背景色。例如,在显示的图像中,红色应该显示一个黑色文本,表示“红色”以指定它。 我有android:entries=“@array/myStrings”,它在设计模式下显示第一种颜色的文本(没有颜色,纯白色背景),但一旦我运行程序,任何颜色的文本都不会显示 strings.xml <resources> <string name="app_name">Color Activity</string>
android:entries=“@array/myStrings”
,它在设计模式下显示第一种颜色的文本(没有颜色,纯白色背景),但一旦我运行程序,任何颜色的文本都不会显示
strings.xml
<resources>
<string name="app_name">Color Activity</string>
<string-array name="myStrings">
<item>Silver</item>
<item>Pink</item>
<item>Red</item>
<item>Orange</item>
<item>Yellow</item>
<item>Green</item>
<item>Blue</item>
<item>Indigo</item>
<item>Violet</item>
<item>Brown</item>
</string-array>
</resources>
颜色活性
银币
粉红色
红色
橙色
黄色的
绿色
蓝色
靛蓝
紫罗兰色
棕色的
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="silver" type="color">#c0c0c0</color>
<color name="pink" type="color">#ffc0cb</color>
<color name="red" type="color">#ff0000</color>
<color name="orange" type="color">#ffa500</color>
<color name="yellow" type="color">#ffff00</color>
<color name="green" type="color">#00ff00</color>
<color name="blue" type="color">#0000ff</color>
<color name="indigo" type="color">#4b0082</color>
<color name="violet" type="color">#7f00ff</color>
<color name="brown" type="color">#654321</color>
<color name="white" type="color">#FFFFFF</color>
<integer-array name="myColors">
<item>@color/silver</item>
<item>@color/pink</item>
<item>@color/red</item>
<item>@color/orange</item>
<item>@color/yellow</item>
<item>@color/green</item>
<item>@color/blue</item>
<item>@color/indigo</item>
<item>@color/violet</item>
<item>@color/brown</item>
</integer-array>
</resources>
#008577
#00574B
#D81B60
#c0c0c0
#ffc0cb
#ff0000
#ffa500
#ffff00
#00ff00
#0000ff
#4b0082
#7f00ff
#654321
#FFFFFF
@颜色/银色
@颜色/粉红色
@颜色/红色
@颜色/橙色
@颜色/黄色
@颜色/绿色
@颜色/蓝色
@颜色/靛蓝
@颜色/紫色
@颜色/棕色
主要活动
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Color;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
public class ColorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final View newBackground;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.myColors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(new ColorAdapter(this));
newBackground = this.getWindow().getDecorView();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<Integer> colors;
colors = new ArrayList<Integer>();
int retrieve []= getResources().getIntArray(R.array.myColors);
for(int i:retrieve)
{
colors.add(i);
}
if(position == 0){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.silver);
}else if(position == 1){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.pink);
}else if(position == 2){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.red);
}else if(position == 3){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.orange);
}else if(position == 4){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.yellow);
}else if(position == 5){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.green);
}else if(position == 6){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.blue);
}else if(position == 7){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.indigo);
}else if(position == 8){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.violet);
}else if(position == 9){
view.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.brown);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
导入androidx.appcompat.app.appcompat活动;
导入android.content.Context;
导入android.graphics.Color;
导入android.media.session.PlaybackState;
导入android.os.Bundle;
导入android.provider.contacts合同;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类ColorActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
新背景的最终视图;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终微调器微调器=findViewById(R.id.Spinner);
ArrayAdapter=ArrayAdapter.createFromResource(
这个,R.array.myColors,android.R.layout.simple\u spinner\u item);
adapter.setDropDownViewResource(
android.R.layout.simple\u微调器\u下拉菜单\u项);
setAdapter(新的ColorAdapter(this));
newBackground=this.getWindow().getDecorView();
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
阵列颜色;
颜色=新的ArrayList();
int retrieve[]=getResources().getIntArray(R.array.myColors);
for(inti:retrieve)
{
颜色。添加(i);
}
如果(位置==0){
视图.setBackgroundResource(R.color.white);
新背景资源(R.color.silver);
}否则如果(位置==1){
视图.setBackgroundResource(R.color.white);
新背景。挫折背景资源(R.颜色。粉红色);
}否则如果(位置==2){
视图.setBackgroundResource(R.color.white);
新背景资源(红色);
}否则如果(位置==3){
视图.setBackgroundResource(R.color.white);
新背景资源(R.color.orange);
}否则如果(位置==4){
视图.setBackgroundResource(R.color.white);
新背景。挫折背景资源(R.颜色。黄色);
}否则如果(位置==5){
视图.setBackgroundResource(R.color.white);
新背景资源(R.color.green);
}否则如果(位置==6){
视图.setBackgroundResource(R.color.white);
newBackground.setBackgroundResource(R.color.blue);
}否则如果(位置==7){
视图.setBackgroundResource(R.color.white);
新背景资源(R.color.indigo);
}否则如果(位置==8){
视图.setBackgroundResource(R.color.white);
新背景。挫折背景资源(R.颜色。紫色);
}否则如果(位置==9){
视图.setBackgroundResource(R.color.white);
新背景资源(R.color.brown);
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
}
}
颜色适配器:
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ColorAdapter extends BaseAdapter {
ArrayList<Integer> colors;
Context context;
public ColorAdapter(Context context){
this.context=context;
colors = new ArrayList<Integer>();
int retrieve []=context.getResources().getIntArray(R.array.myColors);
for(int i:retrieve)
{
colors.add(i);
}
}
@Override
public int getCount() {
return colors.size();
}
@Override
public Object getItem(int args) {
return colors.get(args);
}
@Override
public long getItemId(int args) {
return args;
}
public String getElementFromColors(int position){
String retrieve []=context.getResources().getStringArray(R.array.myColors);
return retrieve[position];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
convertView=inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
TextView txv=(TextView)convertView.findViewById(android.R.id.text1);
txv.setBackgroundColor(colors.get(position));
txv.setTextSize(20f);
txv.setText(getElementFromColors(position));
return convertView;
}
}
导入android.content.Context;
导入android.graphics.Color;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.BaseAdapter;
导入android.widget.SpinnerAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类ColorAdapter扩展了BaseAdapter{
阵列颜色;
语境;
公共颜色适配器(上下文){
this.context=context;
颜色=新的ArrayList();
int retrieve[]=context.getResources().getIntArray(R.array.myColors);
for(inti:retrieve)
{
颜色。添加(i);
}
}
@凌驾
public int getCount(){
返回颜色。size();
}
@凌驾
公共对象getItem(int args){
返回颜色。获取(args);
}
@凌驾
公共长getItemId(int args){
返回args;
}
公共字符串getElementFromColors(int位置){
字符串检索[]=context.getResources().getStringArray(R.array.myColors);
返回检索[位置];
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气机=LayoutFlater.from(上下文);
convertView=充气机。充气(android.R.layout.simple\u spinner\u dropdow
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.myColors, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.myStrings, android.R.layout.simple_spinner_item);
String retrieve[] = context.getResources().getStringArray(R.array.myColors);
String retrieve[] = context.getResources().getStringArray(R.array.myStrings);