Spinner中的文字居中

一、
simple_spinner_item.xml和simple_spinner_dropdown_item.xml其实都是在sdk\platforms\android-18\data\res\layout中的,那么我们可以覆写他们。
比如这样覆写:
Spinner中的文字居中_第1张图片
把TextView中的gravity改一下,这个问题就解决了。
二、
另外,StackOverflow上还有人给出 其他的方法
The component that actually creates the items for the Spinner is the adapter. So you should customize it (by overriding the getView() method) to return centered TextView widgets.
In your case, replace the new ArrayAdapter ... initialization with this code:
ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, NomProjets)
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        return setCentered(super.getView(position, convertView, parent));
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        return setCentered(super.getDropDownView(position, convertView, parent));
    }

    private View setCentered(View view)
    {
        TextView textView = (TextView)view.findViewById(android.R.id.text1);
        textView.setGravity(Gravity.CENTER);
        return view;
    }
};

你可能感兴趣的:(Android篇)