Android: Spinner text alignment to the right but not centered

泄露秘密 提交于 2019-11-29 19:20:58

问题


I finished the English version of my application and now I am working on the Arabic localization. Arabic is a right-to-left language, so I need to adjust a lot of things in my layout, including my spinner display.

I used the same approach mentioned here Android - Text is Pushed to the Left in a Spinner but I set the gravity to right.

Here is my spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_width="fill_parent"
    android:gravity="right" />

Changed my code from

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

to this

adapter.setDropDownViewResource(R.layout.spinner_item);

I want my spinner to look like my old English spinner found below,

But it currently looks like this,

How can I restore the following:

  1. The radio button
  2. The bigger text size
  3. I want it to the right as shown but centered between the two dividers.

NOTE The Arabic webservices aren't finished yet, that's why the data in the image is still in English.

UPDATE

After trying Adil Soomro's suggestion, I get the following,

There is no radiobutton and there is a considerable space between the border and the first letter.

UPDATE

After Adil Soomro's edit, I now have the following,


回答1:


You have to use CheckedTextView, it will solve your all 3 problems.

You will place a layout xml something like this:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
    android:gravity="right|center_vertical"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:textSize="20dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />



回答2:


Try this:

android:layout_gravity="right"



回答3:


change your code

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_width="fill_parent"
    android:gravity="right" />

To

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


来源:https://stackoverflow.com/questions/11341613/android-spinner-text-alignment-to-the-right-but-not-centered

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