Android 動態添加Spinner(.java檔案內實現) 實現 改變spinner 內文字屬性

來源:互聯網
上載者:User

標籤:


動態添加spinner 控制項

Spinner s = new Spinner(this);String []items={"自己定義的要顯示的數組"};my_SpinnerAdapter array_adapter =new my_SpinnerAdapter(this, android.R.layout.simple_spinner_item, items);array_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);s.setAdapter(array_adapter);

 

用內部類 改寫ArrayAdapter
class my_SpinnerAdapter extends ArrayAdapter<String> { Context context; String [] objects; public my_SpinnerAdapter(Context context, int textViewResourceId, String[] items) { super(context, textViewResourceId, items); this.context = context; this.objects = items; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { // 這個函數修改的是spinner點擊之後出來的選擇的部分的字型大小和方式 if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false); } //這裡使用的text1 直接複製過來就行 不用重新起名 否則可能找不到這個文字框 是系統預設的 TextView tv = (TextView) convertView.findViewById(android.R.id.text1); tv.setText(objects[position]); tv.setTextSize(18);// 這裡實現顯示文字的設定 return convertView; } @Override public View getView(int position, View convertView, ViewGroup parent) { //這個函數修改的選擇完spinner中的東西後 顯示在螢幕上的字型的大小 if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate( android.R.layout.simple_spinner_item, parent, false); } //這裡使用的text1 直接複製過來就行 不用重新起名 否則可能找不到這個文字框 是系統預設的 TextView tv = (TextView) convertView.findViewById(android.R.id.text1); tv.setText(objects[position]); tv.setTextSize(18); return convertView; } }

 

Android 動態添加Spinner(.java檔案內實現) 實現 改變spinner 內文字屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.