Android面試簡(jiǎn)錄——組件

組件所在包:android.widget


組件的屬性

  • android:id屬性是必須的嗎?請(qǐng)解釋一下該屬性的作用。
    android:id屬性在XML文件中不一定要指定。
    android:id的作用:
    1. Java代碼中創(chuàng)建組件對(duì)象時(shí),再定義組件時(shí)需要指定android:id對(duì)象;
    2. RelativeLayout中需要確定組件之間的相對(duì)位置,需要指定組件ID。

【拓展】android:id屬性值
設(shè)置方法:
1.@id/屬性值,該屬性已在R類(lèi)中定義;
2.@+id/屬性值,該屬性沒(méi)有事先定義。【建議使用】

  • android:padding和android:layout_margin的區(qū)別
    android:padding用于設(shè)置組件內(nèi)容距離邊緣的距離。
    android:layout_margin用于設(shè)置組件之間的間隔距離。

【個(gè)人發(fā)現(xiàn)】加了layout_這個(gè)屬性就是設(shè)置組件與組件級(jí)別的,沒(méi)加就是設(shè)置組件內(nèi)級(jí)別的。(如果有例外的話請(qǐng)告訴我=。=)

【拓展】分別設(shè)置上、下、左、右4個(gè)方向的間隔距離
1.android:paddingandroid:layout_margin會(huì)同時(shí)設(shè)置上下左右的距離。
2.android:paddingTop,android:paddingBottom,android:paddingLeft,android:paddingRight分別設(shè)置組件內(nèi)上下左右的間隔距離。
3.android:layout_marginTop,android:layout_mrginBottom,android:layout_marginLeft,android:layout_marginRight分別設(shè)置組件間上下左右的間隔距離。

  • 請(qǐng)簡(jiǎn)單描述一下android:gravity和android:layout_gravity屬性的區(qū)別。
    android:gravity用來(lái)設(shè)置組件內(nèi)容相對(duì)于組件的相對(duì)位置。
    android:layout_gravity用來(lái)設(shè)置組件在父組件中的相對(duì)位置。

  • 請(qǐng)說(shuō)出android:layout_weight屬性的功能。
    設(shè)置當(dāng)前組件在水平或者垂直方向上所占空間的大小,屬性值與當(dāng)前組件的寬度和高度成反比。


文本組件

  • 請(qǐng)簡(jiǎn)要說(shuō)出Android SDK支持哪些方式顯示富文本信息,并簡(jiǎn)要說(shuō)明實(shí)現(xiàn)方法。
    1.在TextView組件中使用富文本標(biāo)簽顯示富文本信息。如<font>,<b>等。
    【注意】包含這些標(biāo)簽的文本不能直接作為T(mén)extView.setText方法的參數(shù)值,而要先使用Html.fromHtml方法將這些文本轉(zhuǎn)換成CharSequnence對(duì)象。
    2.使用WebView顯示HTML頁(yè)面。
    3.繼承View或其子類(lèi),覆蓋onDraw方法,在該方法中直接繪制富文本或圖像。
    4.上面3種方法都支持圖片混排效果。
    【注意】方法1在顯示圖像時(shí)需要實(shí)現(xiàn)ImageGetter接口,并通過(guò)ImageGetter.getDrawable方法返回封裝圖像資源的Drawable對(duì)象。
    5.TextView組件中顯示圖像可以使用ImageSpan對(duì)象。
    ImageSpan對(duì)象封裝Bitmap對(duì)象 -> SpannableString對(duì)象封裝ImageSpan對(duì)象 -> TextView.setText(SpannableString對(duì)象)。

【拓展】 在TextView中顯示圖像的辦法

  1. 使用<img>標(biāo)簽
    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對(duì)象
    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對(duì)象的用法
    URLSpan對(duì)象:可以使單擊URL后立刻彈出瀏覽器來(lái)顯示相應(yīng)的界面。
    ClickableSpan類(lèi):自定義單擊URL鏈接動(dòng)作

    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對(duì)象插入富文本改為使用EditText對(duì)象即可。

  • 如何為T(mén)extView組件中顯示的文本添加背景色?
    使用BackgroundColorSpan對(duì)象設(shè)置文字背景色。
    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);

  • 在設(shè)計(jì)電子詞典程序時(shí),當(dāng)用戶輸入單詞時(shí),應(yīng)顯示當(dāng)前輸入單詞的開(kāi)頭的單詞列表。Android SDK中那個(gè)組件可以實(shí)現(xiàn)這個(gè)功能,請(qǐng)寫(xiě)出核心實(shí)現(xiàn)代碼。
    關(guān)鍵點(diǎn):AutoCompleteTextView組件+數(shù)據(jù)庫(kù)與該組件結(jié)合使用
    TextWatcher.afterTextChanged:存放獲取以某些字符開(kāi)頭的單詞列表的代碼塊

    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的子類(lèi) -> Button也可以實(shí)現(xiàn)圖片混排的效果。
    1. Button
    a. android:drawableXxx(Xxx:Left/Top/Right/Bottom):將圖像顯示在文字的周?chē)?br> b. ImageSpan封裝Bitmap對(duì)象 -> SpannableString.setSpan方法設(shè)置ImageSpan對(duì)象 -> Button.setText/Button.append設(shè)置SpannableString對(duì)象
    2. ImageButton
    使用android:src屬性指定圖像文件的資源ID
    3. RadioButton
    和Button一樣的設(shè)置方法

  • 如何用代碼動(dòng)態(tài)改變Button的大小和位置?
    Button是View的子類(lèi),可以使用Button.layout方法動(dòng)態(tài)改變Button的大小和位置。
    Button.layout:4個(gè)參數(shù)——左上角頂點(diǎn)和右下角頂點(diǎn)坐標(biāo)。

  • 怎樣讓一個(gè)顯示圖像的按鈕在不同狀態(tài)顯示不同的圖像?
    drawable資源實(shí)現(xiàn)或者Java代碼實(shí)現(xiàn)。這里主要說(shuō)一下drawable資源實(shí)現(xiàn)的方法。
    在drawable目錄下新建xml文件,然后設(shè)置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資源可以存儲(chǔ)普通的圖像資源,還可以存儲(chǔ)XML圖像資源:
1.圖像狀態(tài)資源:如上
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.圖像級(jí)別資源:如下
<?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設(shè)置級(jí)別在某個(gè)區(qū)間,系統(tǒng)就會(huì)先使用那個(gè)區(qū)間的圖像。


圖像組件

  • 如何實(shí)現(xiàn)圖片的半透明度?
    1.Paint.setAlpha。
    Bitmap對(duì)象裝載圖像 -> View.onDraw(){Canvas.drawBitmap將Bitmap對(duì)象繪制在當(dāng)前View上}
    2.使用<FrameLayout>標(biāo)簽通過(guò)圖層的方式實(shí)現(xiàn)圖像的半透明效果。
    在不透明的圖像上覆蓋一層半透明的膜
    具體代碼:
    方法一:
    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.圖像剪切資源(只能從圖像的一端開(kāi)始截取,沒(méi)有方法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設(shè)置截取的百分比
    ImageView imageView = (ImageView) findViewById(R.id.image);
    ClipDrawable drawable = (ClipDrawable) imageView.getDrawable();
    drawable.setLevel(3000); //從圖像左側(cè)(因?yàn)樵趚ml文件中設(shè)置了"horizontal"屬性)截取圖像的30%

  • 如何使用Matrix對(duì)象旋轉(zhuǎn)和縮放圖像?
    1.Matrix.setRotate~旋轉(zhuǎn)圖像
    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);
    ...


進(jìn)度組件

  • ProgressBar的進(jìn)度條是否可以修改?
    改變ProgressBar的進(jìn)度條顏色(背景顏色,第一級(jí)進(jìn)度條顏色,第二級(jí)進(jìn)度條顏色)。
    修改方案:使用圖層列表(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>標(biāo)簽中使用android:progressDrawable指定以上資源ID。

【拓展】實(shí)現(xiàn)更絢麗的進(jìn)度條
layer-listShape資源結(jié)合,實(shí)現(xiàn)更絢麗的進(jìn)度條。
設(shè)置圓角和漸變色效果:

  <? 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>
  • 如何實(shí)現(xiàn)垂直進(jìn)度條?
    Android SDK沒(méi)有提供垂直進(jìn)度條組件。
    使用圖像剪切資源。
    <? 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的子類(lèi)將數(shù)據(jù)顯示在ListView組件中?
    BaseAdapter的抽象方法:
    1.getItem ~ 返回與當(dāng)前列表項(xiàng)相關(guān)的Object對(duì)象,可不編寫(xiě)
    2.getItemId ~ 返回列表項(xiàng)的ID,long類(lèi)型的值,可不編寫(xiě)
    3.getCount ~ 返回列表數(shù)據(jù)的總數(shù),必須編寫(xiě)
    4.getView ~ 返回在當(dāng)前列表項(xiàng)顯示的View對(duì)象,必須編寫(xiě)
    public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) { //沒(méi)有View對(duì)象可以利用,必須創(chuàng)建新的View對(duì)象
    convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
    return convertView;
    }

  • 如何對(duì)GridView,ListView等列表組件中的數(shù)據(jù)進(jìn)行增、刪、改、查操作?
    列表組件采用MVC格式,必須在修改數(shù)據(jù)后使用notifyDataSetInvalidated方法通知列表組件#刷新#數(shù)據(jù)。

  • 如何在ListView組件中顯示數(shù)據(jù)庫(kù)中的數(shù)據(jù)?
    簡(jiǎn)單數(shù)據(jù):SimpleCursorAdapter
    復(fù)雜數(shù)據(jù):編寫(xiě)繼承CursorAdapter的類(lèi) -> 在newView方法中創(chuàng)建新的列表項(xiàng)View對(duì)象 -> 在bindView中衛(wèi)相應(yīng)的組件賦值。

  • 如何改變ListView的背景色?
    1.采用<listView>標(biāo)簽的android:listSelector屬性
    2.使用ListView.setSelector

  • 如果要做一個(gè)文件管理器,使用GridView組件顯示文件列表,有的文件需要顯示縮略圖,應(yīng)如何優(yōu)化顯示過(guò)程?
    使用任務(wù)隊(duì)列技術(shù)。
    在getView方法中要顯示某個(gè)圖像文件的縮略圖,可以先將該任務(wù)添加到任務(wù)隊(duì)列中,然后使用另外一個(gè)線程不斷掃描任務(wù)隊(duì)列,并處理未完成的任務(wù)。當(dāng)處理完某個(gè)任務(wù)后,可以刷新GridView組建來(lái)顯示該圖像文件的縮略圖。當(dāng)然,網(wǎng)絡(luò)數(shù)據(jù)也可以采用這種方式進(jìn)行優(yōu)化。
    具體步驟:使用數(shù)組/List對(duì)象建立任務(wù)隊(duì)列和數(shù)據(jù)緩沖 -> 將getView中比較耗時(shí)的操作加入到該隊(duì)列中 -> 另外一個(gè)線程執(zhí)行任務(wù)隊(duì)列中的任務(wù) -> BaseAdapter.notifyDataSetChanged刷新列表數(shù)據(jù)。
    代碼如下:
    public class ScanTaskThread extends Thread {
    public void run() {
    try {
    //掃描任務(wù)隊(duì)列以獲取并處理任務(wù)
    ... ...
    Thread.sleep(100);
    } catch (Exception e) {

            }
        }
    }
    
  • 如何為L(zhǎng)istView組件加上加速滑塊?
    android:fastScrollEnabled屬性設(shè)為true -> Java代碼中調(diào)用ListView.setFastEnabled(true)方法。
    ListView組建沒(méi)有提供修改快速滑塊圖像的API,不能直接修改快速滑塊圖像,可以通過(guò)反射技術(shù)修改快速滑塊圖像:
    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的子類(lèi)都是容器組件。
    五大布局組件,GridView,Gallery,ListView。
  • 如何使容器內(nèi)的組件可以水平和垂直滑動(dòng)?
    將ScrollView和HorizontalScrollView結(jié)合使用:
    在<ScrollView>標(biāo)簽中使用<HorizontalScrollView>標(biāo)簽,或者相反。
  • 如何使Gallery循環(huán)顯示圖像?
    使BaseAdapter.getCount返回一個(gè)較大的值,當(dāng)BaseAdapter.getView方法中不能通過(guò)position參數(shù)獲得數(shù)據(jù)的位置,需要將position參數(shù)值對(duì)n(數(shù)組長(zhǎng)度)取余,并將余數(shù)作為新的數(shù)據(jù)位置。

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,581評(píng)論 25 708
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 6,554評(píng)論 0 17
  • 四大應(yīng)用程序組件 Android中的窗口:Activity 如何配置Activity才能讓程序啟動(dòng)時(shí)將該Activ...
    nancymi閱讀 1,128評(píng)論 3 10
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 46,907評(píng)論 22 665
  • 你是低頭族嗎? 低頭族,是指如今無(wú)論何時(shí)何地,人們都作“低頭看屏幕”狀,有的看手機(jī),有的掏出平板電腦或筆記本電腦上...
    林玨good閱讀 379評(píng)論 0 0