【发布时间】:2018-04-19 20:36:25
【问题描述】:
我在同一个活动中放置了两个微调器,一个用于 City,另一个用于 Town。当用户选择城市时,Town 微调器应根据所选城市填充项目。
我的问题是,背景和文本的颜色总是与第一个不同,但是它们具有相同的样式和属性。我找不到任何合乎逻辑的解决方案,也没有在网络上找到任何建议。
您对原因或解决方案有任何想法吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:onClick="pickDate"
android:text="Select date" />
<TextView
android:id="@+id/viewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Distribution date"
android:textAlignment="center" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="The governorate"
android:textAlignment="center" />
<Spinner
android:id="@+id/static_spinner"
style="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The district"
android:textAlignment="center" />
<Spinner
android:id="@+id/district_spinner"
style="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Place ID" />
<EditText
android:id="@+id/plcID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:onClick="next"
android:text="Next" />
</LinearLayout>
</LinearLayout>
MainInfoActivity 文件
public class MainInfoActivity extends Activity {
TextView textView, plcID;
Spinner staticSpinner;
Spinner dynamicSpinner;
Spinner districtSpinner;
CharSequence[] arrayDistrict;
ArrayAdapter<CharSequence> districtAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_info_activity);
textView = (TextView) findViewById(R.id.viewDate);
plcID = (TextView) findViewById(R.id.plcID);
//Drop down lists
staticSpinner = (Spinner) findViewById(R.id.static_spinner);
// Create an ArrayAdapter using the string array and a default spinner
final ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(
this, R.array.governorate_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
districtSpinner = (Spinner) findViewById(R.id.district_spinner) ;
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view1, int i, long l) {
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
districtSpinner.setAdapter(districtAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
【问题讨论】:
-
你能发布你的部分代码和xml文件吗?
-
我贴了xml,java代码很长不知道具体应该贴哪一部分
-
你能把设置 Spinner 列表适配器的部分 java 代码贴出来吗?
-
@Ruan_Lopes 请向下滚动上面的代码,设置适配器的部分在那里
标签: android spinner android-spinner android-theme appearance