Android Spinner Set dropdown text direction to RTL

青春壹個敷衍的年華 提交于 2020-01-15 10:25:27

问题


How to set spinner dropdown text direction to rtl? I know for regular texts we can use android:textDirection="rtl" but it does not work for spinner dropdown text. Should I implement it in popup theme? How?

Thanks


回答1:


Try This -

Add below code in Activity

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
R.array.planets_array,
//        android.R.layout.simple_spinner_item);
    R.layout.textview);
//    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.textview);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

textview.xml

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />

Change Gravity as per your requirement... Hope this will help...




回答2:


First of all make sure that you enabled rtl support in AndroidManifest.xml

    <application
    ...
    android:supportsRtl="true"
    ...>

after that use this in your Spinner definition :

android:layoutDirection="rtl"
android:textDirection="rtl"


来源:https://stackoverflow.com/questions/53909697/android-spinner-set-dropdown-text-direction-to-rtl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!