android原生的Spinner提供了下拉列表選項(xiàng)框,但在一些流行的APP中,原生的Spinner似乎不太受待見(jiàn),而通常會(huì)有下圖所示的下拉列表選項(xiàng)框:
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)吉陽(yáng),十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18980820575初始化狀態(tài):
點(diǎn)擊彈出下拉選擇選項(xiàng)框:
選中后:
注意那個(gè)指示箭頭,如果把這個(gè)控件寫(xiě)的比較精細(xì)的話,在下拉-選擇-復(fù)位過(guò)程中箭頭是應(yīng)該有動(dòng)畫(huà)旋轉(zhuǎn)效果的。
這種樣式的選擇框?qū)崿F(xiàn)方案很多,并且常見(jiàn)、常用,我自己寫(xiě)了一個(gè),我寫(xiě)的這個(gè)例子的代碼運(yùn)行結(jié)果就是上圖所示。
首先是MainActivity.Java:
package zhangphil.app; import android.content.Context; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.ListPopupWindow; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ListPopupWindow listPopupWindow = null; private ImageView arrowImageView; private TextView chooseText; private LinearLayout linearLayout; private SortAadapter adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout= (LinearLayout) findViewById(R.id.linearLayout); arrowImageView = (ImageView)findViewById(R.id.arrow); chooseText= (TextView) findViewById(R.id.chooseText); linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showListPopupWindow(linearLayout); } }); } private class SortAadapter extends ArrayAdapter { private String[] strs = {"最新", "推薦", "全部", "最熱"}; private LayoutInflater inflater; private int res; public SortAadapter(Context context, int resource) { super(context, resource); inflater = LayoutInflater.from(context); res = resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = inflater.inflate(res, null); TextView text = (TextView) convertView.findViewById(android.R.id.text1); text.setText(getItem(position)); text.setTextColor(Color.WHITE); text.setTextSize(10); convertView.setBackgroundColor(Color.RED); return convertView; } @Override public String getItem(int position) { return strs[position]; } @Override public int getCount() { return strs.length; } } public void showListPopupWindow(View view) { if (listPopupWindow == null) listPopupWindow = new ListPopupWindow(this); if (adapter == null) { adapter = new SortAadapter(this, android.R.layout.simple_list_item_1); // ListView適配器 listPopupWindow.setAdapter(adapter); // 選擇item的監(jiān)聽(tīng)事件 listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(getApplicationContext(), adapter.getItem(pos), Toast.LENGTH_SHORT).show(); chooseText.setText(adapter.getItem(pos)); listPopupWindow.dismiss(); } }); listPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { //旋轉(zhuǎn)0度是復(fù)位ImageView arrowImageView.animate().setDuration(500).rotation(0).start(); } }); } // ListPopupWindow的錨,彈出框的位置是相對(duì)當(dāng)前View的位置 listPopupWindow.setAnchorView(view); listPopupWindow.setVerticalOffset(dip2px(this,5)); // 對(duì)話框的寬高 listPopupWindow.setWidth(view.getWidth()); listPopupWindow.setModal(true); listPopupWindow.show(); arrowImageView.animate().setDuration(500).rotation(180).start(); } public static int dip2px(Context context, float dipValue) { float sDensity = context.getResources().getDisplayMetrics().density; final float scale = sDensity; return (int) (dipValue * scale + 0.5f); } }