組件所在包:android.widget
組件的屬性
- android:id屬性是必須的嗎?請解釋一下該屬性的作用。
android:id屬性在XML文件中不一定要指定。
android:id的作用:
1. Java代碼中創建組件對象時,再定義組件時需要指定android:id對象;
2. RelativeLayout中需要確定組件之間的相對位置,需要指定組件ID。
【拓展】android:id屬性值
設置方法:
1.@id/屬性值
,該屬性已在R類中定義;
2.@+id/屬性值
,該屬性沒有事先定義。【建議使用】
- android:padding和android:layout_margin的區別
android:padding用于設置組件內容距離邊緣的距離。
android:layout_margin用于設置組件之間的間隔距離。
【個人發現】加了
layout_
這個屬性就是設置組件與組件級別的,沒加就是設置組件內級別的。(如果有例外的話請告訴我=。=)
【拓展】分別設置上、下、左、右4個方向的間隔距離
1.android:padding
和android:layout_margin
會同時設置上下左右的距離。
2.android:paddingTop
,android:paddingBottom
,android:paddingLeft
,android:paddingRight
分別設置組件內上下左右的間隔距離。
3.android:layout_marginTop
,android:layout_mrginBottom
,android:layout_marginLeft
,android:layout_marginRight
分別設置組件間上下左右的間隔距離。
請簡單描述一下android:gravity和android:layout_gravity屬性的區別。
android:gravity用來設置組件內容相對于組件的相對位置。
android:layout_gravity用來設置組件在父組件中的相對位置。請說出android:layout_weight屬性的功能。
設置當前組件在水平或者垂直方向上所占空間的大小,屬性值與當前組件的寬度和高度成反比。
文本組件
- 請簡要說出Android SDK支持哪些方式顯示富文本信息,并簡要說明實現方法。
1.在TextView組件中使用富文本標簽顯示富文本信息。如<font>,<b>等。
【注意】包含這些標簽的文本不能直接作為TextView.setText方法的參數值,而要先使用Html.fromHtml方法將這些文本轉換成CharSequnence對象。
2.使用WebView顯示HTML頁面。
3.繼承View或其子類,覆蓋onDraw方法,在該方法中直接繪制富文本或圖像。
4.上面3種方法都支持圖片混排效果。
【注意】方法1在顯示圖像時需要實現ImageGetter接口,并通過ImageGetter.getDrawable方法返回封裝圖像資源的Drawable對象。
5.TextView組件中顯示圖像可以使用ImageSpan對象。
ImageSpan對象封裝Bitmap對象 -> SpannableString對象封裝ImageSpan對象 -> TextView.setText(SpannableString對象)。
【拓展】 在TextView中顯示圖像的辦法
- 使用
<img>
標簽
CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = getResources().getDrawable(getResourceId(source));
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}
}, null);
textView.setText(charSequence); - 使用
ImageSpan
對象
Bitmap bitmap = BitmapFacttory.decodeResource(getResources(), R.drawable.icon);
ImageSpan imageSpan = new ImageSpan(this, bitmap);
SpannableString spannableString = new SpannableString("icon");
spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
-
如何使用TextView組件單擊鏈接后彈出自定義的Activity?
考查android.text.style包中Span對象的用法
URLSpan對象:可以使單擊URL后立刻彈出瀏覽器來顯示相應的界面。
ClickableSpan類:自定義單擊URL鏈接動作String text = "顯示Activity" SpannableString spannableString = new SpannableString(text); spannableString.setSpan(new ClickableSpan() { @Override public void onClick(View widgt) { Intent intent = new Intent(Main.this, Activity.class); startActivity(intent); } }, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setMovementMethod(LinkMovementMethod.getInstance());
【拓展】在EditText組件中插入表情圖像?
只需要將以上在使用TextView對象插入富文本改為使用EditText對象即可。
如何為TextView組件中顯示的文本添加背景色?
使用BackgroundColorSpan對象設置文字背景色。
TextView textView = (TextView) findViewById(R.id.textView);
String text = "帶顏色的文本";
SpannableString spannableString = new SpannableString(text);
int start = 0;
int end = 7;
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.getSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);-
在設計電子詞典程序時,當用戶輸入單詞時,應顯示當前輸入單詞的開頭的單詞列表。Android SDK中那個組件可以實現這個功能,請寫出核心實現代碼。
關鍵點:AutoCompleteTextView組件+數據庫與該組件結合使用
TextWatcher.afterTextChanged:存放獲取以某些字符開頭的單詞列表的代碼塊public void afterTextChanged(Editable s) { Cursor cursor = database.rawQuery( "select english as _id from t_words where english like ?", new String[] {s.toString() + "%"}); DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this, cursor, true); autoCompleteTextView.setAdapter(dictionaryAdapter); }
按鈕組件
在按鈕上顯示圖像的方法有哪些?
Button是TextView的子類 -> Button也可以實現圖片混排的效果。
1. Button
a. android:drawableXxx(Xxx:Left/Top/Right/Bottom):將圖像顯示在文字的周圍
b. ImageSpan封裝Bitmap對象 -> SpannableString.setSpan方法設置ImageSpan對象 -> Button.setText/Button.append設置SpannableString對象
2. ImageButton
使用android:src屬性指定圖像文件的資源ID
3. RadioButton
和Button一樣的設置方法如何用代碼動態改變Button的大小和位置?
Button是View的子類,可以使用Button.layout方法動態改變Button的大小和位置。
Button.layout:4個參數——左上角頂點和右下角頂點坐標。怎樣讓一個顯示圖像的按鈕在不同狀態顯示不同的圖像?
drawable資源實現或者Java代碼實現。這里主要說一下drawable資源實現的方法。
在drawable目錄下新建xml文件,然后設置Button:background屬性指向該xml文件即可。
xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/pressed"
/>
<item
android:state_focused="true"
android:drawable="@drawable/focused"
/>
<item
android:drawable="@drawable/normal"
/>
</selector>
【拓展】drawable資源
drawable資源可以存儲普通的圖像資源,還可以存儲XML圖像資源:
1.圖像狀態資源:如上
2.淡入淡出資源:如下
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/lamp_off"/>
<item android:drawable="@drawable/lamp_on"/>
</transition>
3.圖像級別資源:如下
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/lamp_off"
android:minLevel="6"
android:maxLevel="10"
/>
<item
android:drawable="@drawable/lamp_on"
android:minLevel="12"
android:maxLevel="20"
/>
</level-list>
在Java文件中使用setImageLevel或setLevel設置級別在某個區間,系統就會先使用那個區間的圖像。
圖像組件
如何實現圖片的半透明度?
1.Paint.setAlpha。
Bitmap對象裝載圖像 -> View.onDraw(){Canvas.drawBitmap將Bitmap對象繪制在當前View上}
2.使用<FrameLayout>標簽通過圖層的方式實現圖像的半透明效果。
在不透明的圖像上覆蓋一層半透明的膜
具體代碼:
方法一:
InputStream is = getResources().openRawResource(R.drawable.image);
Bitmap bitmap = BitmapFactory.decodeStream(is);
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setAlpha(180);
canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight()), new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
paint);
}
方法二:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFFF" />
</FrameLayout>如何在ImageView組件中顯示圖像的一部分?
1.直接截取圖像
2.圖像剪切資源(只能從圖像的一端開始截取,沒有方法1靈活)
具體代碼:
方法一:
Bitmap smallBitmap = Bitmap.createBitmap(sourceBitmap, 20, 20, 100, 100);
imageView.setImageBitmap(smallBitmap);
方法二:
1.在res/drawable目錄中建立xml文件
<? xml version="1.0" encoding="utf-8" ?>
<clip xmlns:android="http://schema.android.com/apk/res/android"
android:drawable="@drawable/android"
android:clipOrientation="horizontal"
android:gravity="left" />
</clip>
2.在Java代碼中使用ClipDrawable.setLevel設置截取的百分比
ImageView imageView = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageView.getDrawable();
drawable.setLevel(3000); //從圖像左側(因為在xml文件中設置了"horizontal"屬性)截取圖像的30%如何使用Matrix對象旋轉和縮放圖像?
1.Matrix.setRotate~旋轉圖像
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.dog)).getBitmap();
Matrix matrix = new Matrix();
matrix.setRotate(45);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(bitmap);
2.Matrix.setScale~縮放圖像
...
matrix.setScale((float)0.5, (float)0.5);
...
進度組件
- ProgressBar的進度條是否可以修改?
改變ProgressBar的進度條顏色(背景顏色,第一級進度條顏色,第二級進度條顏色)。
修改方案:使用圖層列表(layer-list)資源修改顏色。
res/drawable中新建progressbar.xml文件:
<? xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schema.android.com/apk/res/android" >
<item
android:id="@android:id/background"
android:drawable="@drawable/bg" />
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/secondary" />
<item
android:id="@android:id/progress"
android:drawable="@drawable/progress" />
</layer-list>
在<ProgressBar>標簽中使用android:progressDrawable指定以上資源ID。
【拓展】實現更絢麗的進度條
layer-list
和Shape
資源結合,實現更絢麗的進度條。
設置圓角和漸變色效果:
<? xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schema.android.com/apk/res/android" >
<item android:id="@android:id/background" >
<shape>
<corners android:radius="10dp" />
<gradient
android:startColor="#FFFF0000"
android:centerColor="#FF880000"
android:centerY="0.75"
android:endColor="#FF110000"
android:angle="270" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress" >
<clip>
<shape>
<corners android:radius="10dp" />
<gradient
android:startColor="#FF00FF00"
android:centerColor="#FF00FF00"
android:centerY="0.75"
android:endColor="FF00FF00"
android:angle="270" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress" >
<clip>
<shape>
<corner android:radius="10dp" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
- 如何實現垂直進度條?
Android SDK沒有提供垂直進度條組件。
使用圖像剪切資源。
<? xml version="1.0" encoding="utf-8" ?>
<clip xmlns:android="http://schema.android.com/apk/res/android"
android:drawable="@drawable/android"
android:clipOrientation="vertical"
android:gravity="left" />
</clip>
Java代碼:
ImageView imageView = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageView.getDrawable();
drawable.setLevel(3000);
列表組件
如何使用BaseAdapter的子類將數據顯示在ListView組件中?
BaseAdapter的抽象方法:
1.getItem ~ 返回與當前列表項相關的Object對象,可不編寫
2.getItemId ~ 返回列表項的ID,long類型的值,可不編寫
3.getCount ~ 返回列表數據的總數,必須編寫
4.getView ~ 返回在當前列表項顯示的View對象,必須編寫
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) { //沒有View對象可以利用,必須創建新的View對象
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
return convertView;
}如何對GridView,ListView等列表組件中的數據進行增、刪、改、查操作?
列表組件采用MVC格式,必須在修改數據后使用notifyDataSetInvalidated方法通知列表組件#刷新#數據。如何在ListView組件中顯示數據庫中的數據?
簡單數據:SimpleCursorAdapter
復雜數據:編寫繼承CursorAdapter的類 -> 在newView方法中創建新的列表項View對象 -> 在bindView中衛相應的組件賦值。如何改變ListView的背景色?
1.采用<listView>標簽的android:listSelector屬性
2.使用ListView.setSelector-
如果要做一個文件管理器,使用GridView組件顯示文件列表,有的文件需要顯示縮略圖,應如何優化顯示過程?
使用任務隊列技術。
在getView方法中要顯示某個圖像文件的縮略圖,可以先將該任務添加到任務隊列中,然后使用另外一個線程不斷掃描任務隊列,并處理未完成的任務。當處理完某個任務后,可以刷新GridView組建來顯示該圖像文件的縮略圖。當然,網絡數據也可以采用這種方式進行優化。
具體步驟:使用數組/List對象建立任務隊列和數據緩沖 -> 將getView中比較耗時的操作加入到該隊列中 -> 另外一個線程執行任務隊列中的任務 -> BaseAdapter.notifyDataSetChanged刷新列表數據。
代碼如下:
public class ScanTaskThread extends Thread {
public void run() {
try {
//掃描任務隊列以獲取并處理任務
... ...
Thread.sleep(100);
} catch (Exception e) {} } }
如何為ListView組件加上加速滑塊?
android:fastScrollEnabled屬性設為true -> Java代碼中調用ListView.setFastEnabled(true)方法。
ListView組建沒有提供修改快速滑塊圖像的API,不能直接修改快速滑塊圖像,可以通過反射技術修改快速滑塊圖像:
Field field = AbsListView.class.getDeclaredField("mFastScroller");
field.setAccessible(true);
Object obj = field.get(listView);
field = field.getType().getDeclaredField("mThumbDrawable");
field.setAccessible(true);
Drawable drawable = (Drawable) field.get(obj);
drawable = getResources().getDrawable(R.drawable.image);
field.set(obj, drawable);
容器組件
- Android SDK支持的容器組件?
ViewGroup的子類都是容器組件。
五大布局組件,GridView,Gallery,ListView。 - 如何使容器內的組件可以水平和垂直滑動?
將ScrollView和HorizontalScrollView結合使用:
在<ScrollView>標簽中使用<HorizontalScrollView>標簽,或者相反。 - 如何使Gallery循環顯示圖像?
使BaseAdapter.getCount返回一個較大的值,當BaseAdapter.getView方法中不能通過position參數獲得數據的位置,需要將position參數值對n(數組長度)取余,并將余數作為新的數據位置。
【后記】組件真的是太多了...下個文記述自定義組件和四大應用程序組件。