simple_dropdown_item_1line的问题

来源:互联网 发布:软考初级程序员有用吗 编辑:程序博客网 时间:2024/06/06 13:03
今天在Android Developers查看AutoCompleteTextView 时发现示例代码
[java] view plaincopyprint?
  1. public class CountriesActivity extends Activity {  
  2.     protected void onCreate(Bundle icicle) {  
  3.         super.onCreate(icicle);  
  4.         setContentView(R.layout.countries);  
  5.   
  6.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
  7.                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);  
  8.         AutoCompleteTextView textView = (AutoCompleteTextView)  
  9.                 findViewById(R.id.countries_list);  
  10.         textView.setAdapter(adapter);  
  11.     }  
  12.   
  13.     private static final String[] COUNTRIES = new String[] {  
  14.         "Belgium""France""Italy""Germany""Spain"  
  15.     };  
  16. }  


中有android.R.layout.simple_dropdown_item_1line,一直只是从名字来判断它是简单的一行下拉项目,而没有好好的去弄清楚这到底是什么。今天决定好好看看这个文件到底是

什么意思。首先从android.R.layout可以看出它是一个layout资源,故直接进入E:\Java\android-sdk-windows\platforms\android-8\data\res\layout(我的android-sdk-windows是安装在java下的;我选的开发版本是android2.2,其API Version是8,故选android-8目录),找到simple_dropdown_item_1line.xml:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:id="@android:id/text1"   
  4.     style="?android:attr/dropDownItemStyle"   
  5.     android:textAppearance="?android:attr/textAppearanceLargeInverse"   
  6.     android:singleLine="true"   
  7.     android:layout_width="match_parent"   
  8.     android:layout_height="?android:attr/listPreferredItemHeight"   
  9.     android:ellipsize="marquee"  
  10.     />   

再进入E:\Java\android-sdk-windows\platforms\android-8\data\res\values找到attr.xml文件可以找到

[xml] view plaincopyprint?
  1. <!--  Default style for drop down items. -->   
  2.   <attr name="dropDownItemStyle" format="reference" />   
  3. <!--  Text color, typeface, size, and style for "large" inverse text.   
  4. Defaults to primary inverse text color. -->   
  5.   <attr name="textAppearanceLargeInverse" format="reference" />   

可以看出此TextView的样式是dropDownItemStyle即默认的下拉条目样式;文本外观是textAppearanceLargeInverse即大反差文本。

ellipsize属性即当文本不能完全显示时,文本的显示方式。有五种样式可选。

ConstantValueDescriptionnone0无样式start1前面文本不显示middle2中间的文本不显示end3末尾的文本不显示marquee4跑马灯风格
 
为了方便观察将示例中的字符串改为: String[] strs={"abcdefg","abcdefghi","abcghijkl","abcdfghjklzxcvbnmqwertyuiopasdfghjklzx"};
测试效果如下: