내 Android 응용 프로그램에서 스피너를 사용하고 있으며 SQLite 데이터베이스의 데이터를 스피너에로드했는데 제대로 작동합니다. 그 코드는 다음과 같습니다.
Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();
list.add("All Lists");
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
list.add(keyList[i]);
cursor.moveToNext();
}
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
이제 스피너 데이터의 텍스트 색과 텍스트 크기를 변경하고 싶습니다. XML 파일의 스피너 태그에 다음 XML 행을 사용했지만 작동하지 않습니다.
android:textColor="@android:color/white"
android:textSize="11dp"
스피너의 텍스트 색상과 텍스트 크기를 어떻게 변경합니까?
답변
스피너 항목에 대한 사용자 지정 XML 파일을 만듭니다.
spinner_item.xml :
이 파일에서 텍스트에 사용자 정의 된 색상과 크기를 제공하십시오.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
이제이 파일을 사용하여 다음과 같은 스피너 항목을 표시하십시오.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
드롭 다운 리소스를 설정할 필요가 없습니다. spinner_item.xml항목을 스피너에 표시하는 데만 소요 됩니다.
답변
간단하고 또렷한 … :
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
답변
모든 스피너가 TextView 항목에 대해 동일한 텍스트 색상을 가질 수있는 경우 다른 방법은 스피너 드롭 다운 항목에 사용자 정의 스타일을 사용하는 것입니다.
에서 res/values/styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
</style>
<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">@color/my_spinner_text_color</item>
</style>
</resources>
res / values / colors.xml에서 사용자 정의 색상을 정의하십시오.
<color name="my_spinner_text_color">#808080</color>
답변
Spinner의 색상 을 변경하는 데 도움이되는 링크는 다음과 같습니다 .
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:textSize="20sp"
android:entries="@array/planets"/>
spinner 항목 spinner_item.xml에 대한 사용자 정의를 사용하여 고유 한 레이아웃 파일을 작성해야합니다 .
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
드롭 다운 목록 항목을 사용자 정의하려면 새 레이아웃 파일을 작성해야합니다. spinner_dropdown_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
그리고 마지막으로 스피너 선언의 또 다른 변화 :
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
그게 다야.
답변
android.support.v7.widget.AppCompatSpinner로 작업하는 경우 스타일을 사용하여 가장 간단한 테스트 솔루션이 있습니다.
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spefcialFx"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:theme="@style/Spinner"
android:entries="@array/special_fx_arrays"
android:textSize="@dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>
그리고 스타일 :
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">@color/white</item>
<item name="android:backgroundTint">@color/red</item>
<item name="android:textSize">14sp</item>
</style>
유일한 단점은 android : backgroundTint가 드롭 다운 화살표와 드롭 다운 배경 모두에 대해 색상을 설정한다는 것입니다.
답변
지연을 방지하려면 onItemSelected리스너뿐만 아니라 액티비티에서 텍스트 속성을 설정해야합니다.onCreate 메서드 (그러나 약간 까다 롭습니다).
특히, onCreate어댑터를 설정 한 후에 이것을 넣어야합니다 .
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
그리고 이것을 넣으십시오 onItemSelected:
((TextView) view).setTextColor(backgroundColor);
전체 예는 다음과 같습니다.
@Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
자세한 내용은 내 질문을 참조하십시오 .
답변
선택한 항목에서만 텍스트 색상을 변경하려면 가능한 해결 방법이 될 수 있습니다. 그것은 나를 위해 일했고 당신을 위해 일해야합니다.
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
}
});