您好,登錄后才能下訂單哦!
android原生的Spinner提供了下拉列表選項框,但在一些流行的APP中,原生的Spinner似乎不太受待見,而通常會有下圖所示的下拉列表選項框:
初始化狀態:
點擊彈出下拉選擇選項框:
選中后:
注意那個指示箭頭,如果把這個控件寫的比較精細的話,在下拉-選擇-復位過程中箭頭是應該有動畫旋轉效果的。
這種樣式的選擇框實現方案很多,并且常見、常用,我自己寫了一個,我寫的這個例子的代碼運行結果就是上圖所示。
首先是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的監聽事件 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() { //旋轉0度是復位ImageView arrowImageView.animate().setDuration(500).rotation(0).start(); } }); } // ListPopupWindow的錨,彈出框的位置是相對當前View的位置 listPopupWindow.setAnchorView(view); listPopupWindow.setVerticalOffset(dip2px(this,5)); // 對話框的寬高 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); } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="wrap_content" android:layout_height="24dp" android:background="@drawable/round_corner_line_radius_3dp" android:orientation="horizontal" android:paddingLeft="8dp" android:paddingRight="8dp" android:layout_centerInParent="true"> <TextView android:id="@+id/chooseText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="分類" android:textColor="@android:color/holo_orange_dark" android:textSize="12dp" /> <ImageView android:id="@+id/arrow" android:layout_width="14dp" android:layout_height="14dp" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/icon" /> </LinearLayout> </RelativeLayout>
LinearLayout的背景需要一個圓角背景res/drawable/round_corner_line_radius_3dp.xml:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape= "rectangle"> <stroke android:color="@android:color/holo_orange_dark" android:width="2px"/> <solid android:color="@android:color/transparent"/> <corners android:radius="3dp" /> </shape>
箭頭icon.png:
附:Android第三方開源下拉框NiceSpinner
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。