Program Tip

Android : Spinner 위젯의 텍스트 색상 속성은 어디에 숨어 있습니까?

programtip 2020. 10. 15. 21:28
반응형

Android : Spinner 위젯의 텍스트 색상 속성은 어디에 숨어 있습니까?


드롭 다운에서 항목을 선택한 후 스피너 버튼에 표시되는 단일 항목의 텍스트 색상을 변경하려고합니다. Android SDK에서 themes.xml 및 styles.xml을 한 시간 동안 자세히 살펴 봤는데 Spinner가 색상 값을 가져 오는 위치를 찾을 수없는 것 같습니다.

명확히하기 위해 드롭 다운 항목의 색상을 변경하지 않고 드롭 다운이 없을 때 스피너의 표시 텍스트 색상을 변경하려고합니다. 스피너의 '버튼'텍스트라고 부를 수있을 것 같습니다.


나는 styles.xml에서 아마도이 비트라고 생각합니다.

<style name="Widget.TextView.SpinnerItem">
    <item name="android:textAppearance">@style/TextAppearance.Widget.TextView.SpinnerItem</item>
</style>
<style name="Widget.DropDownItem.Spinner">
    <item name="android:checkMark">?android:attr/listChoiceIndicatorSingle</item>
</style>

-= 편집 =-결과는 다음과 같습니다. 여기에 이미지 설명 입력

수행 방법은 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MooTheme" parent="android:Theme">
        <item name="android:spinnerItemStyle">@style/MooSpinnerItem</item>
    </style>

    <style name="MooSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
        <item name="android:textAppearance">@style/MooTextAppearanceSpinnerItem</item>
    </style>

    <style name="MooTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
        <item name="android:textColor">#F00</item>
    </style>
</resources>

그런 다음 AndroidManifest.xml의 애플리케이션 태그에 추가하십시오.

android:theme="@style/MooTheme"

네 CaseyB가 맞습니다.

스피너 텍스트 색상을 설정하는 방법은 다음과 같습니다. 간단한 예입니다.

styles.xml

    <style name="Theme.NoTitleBar.WithColoredSpinners" parent="@android:style/Theme.NoTitleBar">
        <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
        <item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
    </style>

    <style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
        <item name="android:textColor">#00FF00</item>
    </style>

    <style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.DropDownItem.Spinner">
        <item name="android:textColor">#FF0000</item>
    </style>

</resources>

그런 다음 매니페스트에서 :

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.NoTitleBar.WithColoredSpinners" >

이제 모든 스피너 외부의 텍스트가 녹색이되고 드롭 다운의 텍스트가 빨간색이됩니다.


저는 다른 간단한 기술을 사용하여 이것을했습니다.

Android res / layout 폴더에서 simple_spinner_item.xml 및 simple_spinner_dropdown_item.xml을 복사하여 프로젝트에 복사하십시오.

그런 다음 다음 줄을 수정하십시오.

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, Android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(Android.R.layout.simple_spinner_dropdown_item);
spinnerSubject.setAdapter(adapter);

같이:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinnerSubject.setAdapter(adapter);

나머지는 간단합니다. 이제 simple_spinner_item.xml을 편집하여 스피너 위젯에서 표시되는 항목 하나의 모양을 편집하고 simple_spinner_dropdown_item.xml을 편집하여 드롭 다운 목록의 모양을 변경할 수 있습니다.

예를 들어 내 활동 레이아웃에는 다음이 포함됩니다.

<Spinner
android:id="@+id/mo_spinnerSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/spinnerset_background" />

내 simple_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:singleLine="true"
android:textColor="@color/custom_white"
android:textSize="16sp" />

simple_spinner_dropdown_item.xml은 다음과 같습니다.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@color/custom_black"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@color/custom_white" />

Spinner 객체에서 setOnItemSelectedListener를 사용할 수 있습니다.

spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        ((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(249, 249, 249));
        // OR ((TextView)parentView.getChildAt(0)).setTextColor(Color.RED);
    }
});

its very simple actually. I was looking for all over you just need to create style and set on spinner

first create your theme in Style.xml

 <style name="spinnerTheme" parent="android:Theme">
    <item name="android:textColor">@color/gray_dark</item>
</style>

then in your xml where you've set your spinner add this :

android:theme="@style/spinnerTheme"

                       <Spinner
                        android:id="@+id/spinner"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:padding="10dp"
                        android:paddingBottom="5dp"
                        android:paddingLeft="10dp"
                        android:layout_span="3"
                        android:layout_weight="1.3"
                        android:theme="@style/spinnerTheme"
                        android:textSize="12sp"
                        android:spinnerMode="dialog"
                        android:clickable="false"/>

Enjoy Coding


I dont think there is a color associated with the text. Its most likely predefined in the android code, might have to just make your own if you want to change the spinner's color.

This would include changing the ondraw() method and you draw the spinner how you would like it to look.

Only thing I think could potentially solve this issue is the style property of the spinner.

Source: http://developer.android.com/reference/android/widget/Spinner.html

This might help:

http://www.designerandroid.com/?p=28


이것은 스피너 항목에 대한 사용자 정의 정의로 자신의 레이아웃 파일을 만들었습니다.

custom_spinner.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/txt_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#ffffff" />

어댑터를 사용하여 스피너 항목 변경

Spinner spinner= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.by_loc_array,R.layout.txt_spinner);
spinner.setAdapter(adapter);

다음과 같이 getView 메서드를 재정 의하여 텍스트 색상을 변경할 수 있습니다.

new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
                @Override
                public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    //change the color to which ever you want                    
                    ((CheckedTextView) view).setTextColor(Color.RED);
                    return view;
              }

참고 URL : https://stackoverflow.com/questions/6159113/android-where-is-the-spinner-widgets-text-color-attribute-hiding

반응형