Spinner 自定义 居中

Spinner 自定义 居中_第1张图片
如何实现以上效果呢
activity_list 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/id_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />

    <Spinner android:id="@+id/ctype"
        android:layout_width="100dp"
        android:layout_height="wrap_content"

        android:gravity="center"
        android:entries="@array/myarray"/>//设置数组源

LinearLayout>

那么item条目如何写呢?


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    >

    <ImageView
        android:id="@+id/id_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:visibility="gone"
        />


    <TextView
        android:id="@+id/id_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/id_image"
        android:text="123"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textColor="@android:color/background_dark"
        android:textSize="15sp"
        />

RelativeLayout>

那么在实现的逻辑中如何实现呢?

package com.tcmces.bill.autobanner;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListAActivity extends Activity implements AdapterView.OnItemSelectedListener {

        private TextView tv = null;

        private Spinner spinner = null;

        // 数据源
        private String[] city = {"北京", "上海", "广州", "深圳", "长沙", "南京"};
        private int[] picId = {R.mipmap.ic_launcher, R.mipmap.banner1, R.mipmap.banner2
                ,R.mipmap.banner3, R.mipmap.banner1, R.mipmap.banner5};
        private List> dataList = null;

        private List list = null;

        private ArrayAdapter adapter = null;

        private SimpleAdapter simpleAdapter = null;

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list);

            initViews();
            setData();

            adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);

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

            simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item,
                    new String[] {"pic", "text"}, new int[]{R.id.id_image, R.id.id_text1});


            spinner.setAdapter(simpleAdapter);

            spinner.setOnItemSelectedListener(this);

            spinner.setDropDownVerticalOffset(30);

        }

        private List> getData() {

            for (int i = 0; i < city.length; i++) {
                Map map = new HashMap<>();
                map.put("pic", picId[i]);
                map.put("text", city[i]);
                dataList.add(map);
            }

            return dataList;
        }


        private void setData() {
            for (int i = 0; i < city.length; i++) {
                list.add(city[i]);
            }
        }

        private void initViews() {
            dataList = new ArrayList<>();
            list = new ArrayList<>();
            tv = (TextView) findViewById(R.id.id_text);
            spinner = (Spinner) findViewById(R.id.ctype);
        }

        @Override
        public void onItemSelected(AdapterView parent, View view, int position, long id) {
            String cityName = (String) adapter.getItem(position);
            tv.setText(cityName);
        }

        @Override
        public void onNothingSelected(AdapterView parent) {

        }
    }

以上就完了? 以上没有结束,不然会有个小bug的 那么怎么才能完美的解决掉呢?
你需要在清单文件中加入一句话

<activity android:name=".ListAActivity" android:theme="@android:style/Theme.Holo.Light.NoActionBar"/>

通过以上即可完美实现我所写的业务逻辑~~

你可能感兴趣的:(Spinner 自定义 居中)