如何在android中创建ICS微调器以获得自定义外观

时间:2014-04-28 18:37:38

标签: java android android-layout android-spinner

我尝试了以下代码,在SO链接Dropdown Spinner outside of actionbar? (IceCream Sandwich style, w/ActionBarSherlock)中给出。像这样创建微调器。没有运气:(

enter image description here

这是代码。什么是最简单的替代方式来开发这样的&在Android 2.2之后获得相同的外观和感觉,我使用Actionbarsherlock lib

MyICSspinner.java

    package com.example.icsspinnerexample;

import android.content.Context;
import android.util.AttributeSet;

import com.actionbarsherlock.internal.widget.IcsSpinner;

public class MyIcsSpinner extends IcsSpinner{

    public MyIcsSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyIcsSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

      }

}

Mainactivity.java

   package com.example.icsspinnerexample;

import java.util.ArrayList;
import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SpinnerAdapter;


public class MainActivity extends SherlockActivity {

    MyIcsSpinner icsspinner;
    Customspinneradapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        icsspinner = (MyIcsSpinner)findViewById(R.id.myspinner);



        SpinnerAdapter adapter = new Customspinneradapter();

        icsspinner.setAdapter(adapter);


    }


}

Customspinneradapter.java

   package com.example.icsspinnerexample;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.DataSetObserver;
import android.text.TextUtils.TruncateAt;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.SpinnerAdapter;

public class Customspinneradapter implements SpinnerAdapter{

    ArrayList<String> datalist = new ArrayList<String>();

    public Customspinneradapter()
    {
        datalist.add("Hello");datalist.add("Hello");datalist.add("Hello");datalist.add("Hello");datalist.add("Hello");datalist.add("Hello");
    }



    LayoutInflater layoutInflater;


    @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        final TextView filterName;
        if (convertView == null) {
          filterName = (TextView) layoutInflater.inflate(R.layout.filter_item, parent, false);
        } else {
          filterName = (TextView) convertView;
        }

        filterName.setText((String)getItem(position));
        return filterName;
      }


    @Override
      public View getDropDownView(int position, View convertView, ViewGroup parent) {
        final TextView filterName;
        if (convertView == null) {
          filterName = (TextView) layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, parent, false);
          filterName.setEllipsize(TruncateAt.END);
        } else {
          filterName = (TextView) convertView;
        }

        filterName.setText((String)getItem(position));
        return filterName;
      }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return datalist.size();
    }


    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return datalist.get(arg0);

    }


    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public int getItemViewType(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }


    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }


    @Override
    public void registerDataSetObserver(DataSetObserver arg0) {
        // TODO Auto-generated method stub

    }


    @Override
    public void unregisterDataSetObserver(DataSetObserver arg0) {
        // TODO Auto-generated method stub

    }

}

布局文件

filter_item.xml

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

<TextView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello"/>

Activity_main

<RelativeLayout 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"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<com.example.icsspinnerexample.MyIcsSpinner
    android:id="@+id/myspinner"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:textAllCaps="true"
android:background="@drawable/abs__spinner_ab_holo_light"
android:textColor="#000000"
android:gravity="center"/>

0 个答案:

没有答案