Android面試簡錄——組件

組件所在包: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:paddingandroid: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中顯示圖像的辦法

  1. 使用<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);
  2. 使用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-listShape資源結合,實現更絢麗的進度條。
設置圓角和漸變色效果:

  <? 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(數組長度)取余,并將余數作為新的數據位置。

【后記】組件真的是太多了...下個文記述自定義組件和四大應用程序組件。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,967評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,273評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,870評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,742評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,527評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,010評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,108評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,250評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,769評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,656評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,853評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,371評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,103評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,472評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,717評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,487評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,815評論 2 372

推薦閱讀更多精彩內容