android自訂Spinner下拉式功能表和AutoCompleteTextView自動顯示的(下拉式清單方塊)樣式

來源:互聯網
上載者:User

在Android中Spinner就是下拉式功能表和AutoCompleteTextView自動顯示的樣式,它相當於HTML中的<select>標籤。
Android中提供的Spinner Widget下拉式功能表和AutoCompleteTextView自動顯示已經非常好用了,樣式也適用,
不過我們還是可以通過定義xml的方式來改變下拉式功能表的樣式。
在layout目錄下建立一個xml檔案,名字隨便(我這裡叫activity_main.xml)。在這個檔案裡面可以定義下拉式功能表的樣式
我們這裡採用TextView來實現。

activity_main.xml:

View Code

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:background="#000000" 6     tools:context=".MainActivity" > 7  8     <AutoCompleteTextView 9         android:id="@+id/actv"10         android:layout_width="fill_parent"11         android:layout_height="wrap_content" />12 13 </RelativeLayout>

自訂的樣式就是在layout下建立一個xml檔案,名字這裡我隨便起。代碼如下:

down.xml:

View Code

1 <TextView xmlns:android="http://schemas.android.com/apk/res/android"2     xmlns:tools="http://schemas.android.com/tools"3     android:id="@+id/contentTextView"4     android:layout_width="fill_parent"5     android:layout_height="wrap_content"6     android:background="#F4FDFF"7     android:textColor="#000000" >8 9 </TextView>

在activity中的調用方法如下:

View Code

 1 package com.example.autocompletetextvewtest; 2  3 import java.util.List; 4  5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.widget.ArrayAdapter; 9 import android.widget.AutoCompleteTextView;10 11 public class MainActivity1 extends Activity {12     private AutoCompleteTextView actv;13 14     @Override15     protected void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.activity_main);18         String[] strings = new String[] { "北京", "北京西站", "北京南站", "上海", "上海浦東機場" };19         actv = (AutoCompleteTextView) findViewById(R.id.actv);20         // 第二個參數是樣式的Layout布局id,第三個參數是:顯示具體樣式的TextView的id21         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,22                 R.layout.down, R.id.contentTextView, strings);23         actv.setThreshold(1);24         actv.setAdapter(adapter);25     }26 27     @Override28     public boolean onCreateOptionsMenu(Menu menu) {29         getMenuInflater().inflate(R.menu.activity_main, menu);30         return true;31     }32 33 }

這就完事了!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.