Android - Text is Pushed to the Left in a Spinner

前端 未结 5 870
生来不讨喜
生来不讨喜 2020-12-31 02:03

I have a spinner with a style. The style only contains a background for the spinner. The problem is,that no matter which image I use for the background, the text of the spin

相关标签:
5条回答
  • 2020-12-31 02:40

    If you want to see the checkboxes in the dropdown menu while using your own layout:

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                mContext, R.array.room_list, R.layout.spinner_layout);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        yourSpinner.setAdapter(adapter);
    

    Please notice the "android" before "R.layout...." in the line where you put the drop down view resource!

    0 讨论(0)
  • 2020-12-31 02:51

    I had this problem as well and the accepted answer did not work to me.

    If this also applies to you, make sure that no parent layout overrides the gravity of the spinner item. In my case, I had to set the gravity of the entire Spinner to right.

    0 讨论(0)
  • 2020-12-31 03:01

    Continuing from my last comment above...

    The following code modifies the Hello Spinner tutorial application to display the spinner text contents centered horizontally, as seen in the following two screenshots.

    res/layout/my_spinner_textview.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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" />
    public class HelloSpinner extends Activity
    {
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        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.my_spinner_textview);
    //    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(R.layout.my_spinner_textview);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
      }
    
      //No other modification needed.

    This hopefully provides enough direction to fix the problem.

    0 讨论(0)
  • 2020-12-31 03:01

    Use android:textAlignment="center" tag on spinner

     <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/state"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"/>
    
    0 讨论(0)
  • 2020-12-31 03:01

    Add gravity in your xml file...

    <Spinner
     android:gravity="center">
    </Spinner>
    

    Or add gravity in your java code...

    Spinner spinner = (Spinner)findViewById(R.id.spinner);
    spinner.setGravity(17);
    //17 = center
    
    0 讨论(0)
提交回复
热议问题