开发者

set view text align at center in spinner in android

开发者 https://www.devze.com 2023-04-06 11:12 出处:网络
view-text-in-the-center-of-the-spinner-when-select-from-the-drop-down-list I want to align the view text of spinner to center. I google it but didn\'t find anything, does anybody knows 开发者_如何学J

view-text-in-the-center-of-the-spinner-when-select-from-the-drop-down-list

I want to align the view text of spinner to center. I google it but didn't find anything, does anybody knows 开发者_如何学JAVAabout this? any help or suggestion are appreciated


Create a adapter for your spinner like this,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.my_spinner_style,array_of_values) {

    public View getView(int position, View convertView,ViewGroup parent) {

        View v = super.getView(position, convertView, parent);

        ((TextView) v).setTextSize(16);

        return v;

    }

    public View getDropDownView(int position, View convertView,ViewGroup parent) {

        View v = super.getDropDownView(position, convertView,parent);

        ((TextView) v).setGravity(Gravity.CENTER);

        return v;

    }

};

Now your layout R.layout.my_spinner_style,

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

Now set this adapter to your spinner,

spinner.setAdapter(adapter);


You need to set your own layout for spinner item.

SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"A", "B", "C"});
spriner.setAdapter(adap);

Where R.layout.spinner_item is a layout with content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:textColor="#000000" 
    android:text="Sample Text" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp"></TextView>


This is very simple. Just create a xml file named as, for example, spinner_item.xml with content as

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:padding="10dp"
android:textAlignment="center"
/>

Now in your Java class, instead of adding Android's default view resource add your custom view resource as

ArrayAdapter<String> adapter= new ArrayAdapter<String>(context, R.layout.spinner_item, myList);
 adapter.setDropDownViewResource(R.layout.spinner_item);

The textAlignment line will do the trick.


Setting textAlignment to center will place the text in center.

android:textAlignment="center"

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/spinner_areas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:visibility="visible"
            android:gravity="center"
            android:textAlignment="center"/>


This works without layout changes

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               ((TextView) adapterView.getChildAt(0)).setGravity(Gravity.CENTER);
        }
 });


Add this line in Spinner

android:textAlignment="center"

Voila!


Late to the game, but I wanted to add this for future users just because I found it a simple solution for my problem. It allowed me to center simple text without creating a custom layout. The way I did this was using padding

<Spinner
    android:id="@+id/example_spinner"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp" />

I was successful in centering my text within my spinner using this method. This will likely only work for simple cases, but you would probably want to use a custom layout for more complex cases anyway. This is just to keep your xml files down and avoid having to use a custom layout just to center a simple spinner.


Simple as this!

<Spinner
 android:textAlignment="center"
/>


I found it slightly easier to edit the theme than to create an adapter. In my fragment_first.xml I have my spinner

<Spinner
    android:theme="@style/mySpinnerStyle"
    android:id="@+id/spnnr"
    android:layout_width="350dp"
    android:layout_height="75dp"
    android:layout_marginBottom="32dp"
    android:spinnerMode="dropdown"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

I then created a new @style resource in values (recommended by Kotlin).

<style name="mySpinnerStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textSize">@dimen/ssp</item>
    <item name="android:textAlignment">center</item>

</style>

<dimen name="ssp">24sp</dimen>

points to Farid here for pointing me along. https://stackoverflow.com/a/64193198/15078702


Let me complete answer. It is also important to make space in Spinner.

Wrong:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Right:

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号