Android UI設計總結

來源:互聯網
上載者:User

tyle="margin:20px 0px 0px; text-align:left; color:rgb(54,46,43); line-height:26px; font-family:Arial; font-size:14px">

半透明<Buttonandroid:background="#e0000000" ... />
透明<Buttonandroid:background="#00000000" .../>

其他透明度:在RGB的值前面加上數字(100以內),如#90436EEE(RGB為436EEE),#50436EEE,根據值的大小呈現不同的顯示效果,值越小透明度越高。

2.設定按鈕背景圖片或顏色:

btn.setBackgroundResource(R.drawable.comment_sel);

btn.setTextColor(getResources().getColor(R.color.blue));

3.文字換行:&#x000A;(要加上分號).

4.布局時,不宜做過多的嵌套,否則容易報堆疊溢位異常。

5.9.png圖片最好放在drawable-hdpi檔案夾中,否則可能顯示時可能會出現黑色的外邊線。

6.android進程優先順序:foreground Process(前台進程)>Visable Process(不在最上層顯示,但也沒有完全隱藏,比如彈出一個對話方塊式的Activity)>ServiceProcess>Background Process>Empty Process。

7.Android進程啟動:PackageServiceàPackageManagerà載入manifest設定檔—>讀取MAIN和LAUNCHERà映射類的執行個體。

8.TextView設定符號時(如問號),要在英文鍵盤環境下輸入,否則會被顯示為省略符號。

9.EditText追加字元:

edit.append("0");

edit.getText().insert(edit.getSelectionStart(),"0");

10. EditText去掉預設的邊框:android:background="@null"或者 android:background="#00000000


11.weight的使用:weight的意義在於,對於父控制項,若子控制項能夠完全顯示,先分配沒有設定weight的控制項,對於有weight屬性的控制項,則根據weight的值等分布局的剩餘部分。如右圖:都採用RelativeLayout左中右布局,但是由於文字的字數不同結果出現第二個布局中文字換行。解決方案:對中間的TextView設定weight = 1;外層採用LinearLayout或其他都可以達到需要的效果。

12.轉字串時,優先選用String.valueOf。直接用toString時最好做一個非null判斷。二者區別可參考源碼。

13.android工程中如果出現(所有的)資源找不到異常,可能是圖片問題,檢查9.png是否有問題。

14.對LinearLayout(或其他widget)整個控制項設定onclick事件:

(1).XML中設定CliClickable=true(也可以不用設定,區別在於設定為true之後必須在代碼中註冊監聽事件,否則點擊後程式會崩潰),然後直接重寫onClick方法;

(2)XML中設定onClick=”name(響應事件的名稱)” ,在Activity中寫一個name(View v)的方法執行點擊後的操作。

15. dialog的WindowLeaked異常:

AlertDialogdialog = new AlertDialog.Builder(this);

dialog.show();

finish();

android 上的dialog.show不會阻塞調用線程(如UI線程),導致dialog尚未dismiss或者cancel之前,dialog所依賴的context被關閉,因此出現windowleaked。但是此問題並不影響程式繼續運行。

解決辦法:在dialog的OnClickListener事件中先dismiss(onClick中有一個DialogInterface參數,直接調用此DialogInterface的dismiss即可)後再finish activity(點擊按鈕時先調用dialog的dismiss()方法,然後調用Activity的finish()方法)。

16.xml中有EditText時,彈出鍵盤時導致頁面向上收縮:

解決辦法:在manifest.xml中對應的Activity設定android:windowSoftInputMode="adjustPan";並且如果該xml中有ListView時,不能設定android:fastScrollEnabled="true"android:focusable="true"(有待再次驗證)。

17.若LinearLayout設定OnclickListener點擊事件,則該LinearLayout中不宜放置Button按鈕,否則當點擊整個LinearLayout時,Button所佔的地區無法執行點擊命令。(也許可以通過設定focusable解決)

18. EditText的getText方法不會返回null(因此調用getText不必作mull判斷)。

19.TextView設定字型粗體:

在xml檔案中使用android:textStyle=”bold” 可以將英文設定成粗體,

但是不能將中文設定成粗體,

將中文設定成粗體的方法是:

TextView tv =(TextView)findViewById(R.id.TextView01);

TextPaint tp = tv.getPaint();

tp.setFakeBoldText(true);

20.TimePicker,DatePicker:根據鍵盤輸入動態更新日期

使用TimePicker,DatePicker時無法屏蔽軟鍵盤,因此使用者可以通過鍵盤輸入日期。

假設有一個確定按鈕。當使用者通過鍵盤手動輸入日期,點擊確定,結果無法取到正確的日期值。解決方案:在findviewbyid找到TimePicker,DatePicker時首先調用它們的requestFocus(),點擊確定時調用clearFocus()。

 

21.設定全屏 :

1)setContentView之前:

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

2)自訂style:<style name="full_screen">

        <itemname="android:windowNoTitle">true</item>

      <itemname="android:windowFullscreen">android:windowNoTitle</item>

</style>

對應的Activity設定android:theme=”@style/full_screen”

3)直接在Activity中設定android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

22.使View置中:

利用weight屬性,可以在上下或左右各添加空View:

<View       android:layout_width="0dip"

           android:layout_height="0dp"

            android:layout_weight="1"

            />

23.Fragment中嵌套ViewFlipper時,滑動觸摸無響應:

         在onCreateView中,對由inflate()得到的View設定view.setOnTouchListener(this);

   在重寫onTouch方法時返回true(而不再是detector.onTouchEvent(event))。

24.設定圖片使其充滿ImageView控制項:

   為了匹配ImageView的width和height屬性(如:fill_parent),則設定scaleType=”fitXY”.

25.android的簽名期限如果到期,項目就會報錯,無法打包,不能通過編譯。

26.在Adapter使用Intent啟動Activity:

         在構造器中傳入Context參數,用Context.startActivity()啟動Activity。

27.View設定padding和margin:

         Padding:直接調用setPadding();

         Margin:先構造出LayoutParams params = newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);之後params.setMargins(10,1 0,1 0, 10);最後

view. setLayoutParams(params);

28.軟鍵盤imeOptions的用法:

android:imeOptions="flagNoExtractUi"  //使軟鍵盤不全螢幕顯示,只佔用一部分螢幕

同時,這個屬性還能控制項軟鍵盤右下角按鍵的顯示內容,預設情況下為斷行符號鍵

android:imeOptions="actionNone"  //輸入框右側不帶任何提示

android:imeOptions="actionGo"    //右下角按鍵內容為'開始'

android:imeOptions="actionSearch"  //右下角按鍵為放大鏡圖片,搜尋

android:imeOptions="actionSend"    //右下角按鍵內容為'發送'

android:imeOptions="actionNext"   //右下角按鍵內容為'下一步'

android:imeOptions="actionDone"  //右下角按鍵內容為'完成'

29.EditText設定字元長度限制:  

XML: android:maxLength=“15

Code: editText.setFilters(newInputFilter[]{new InputFilter.LengthFilter(15});

30. java.lang.InstantiationException:can't instantiate class com.sns.ui.Login$UserLogin; no empty constructor

內部類UserLogin聲明成static。

31. 判斷當前為飛航模式:

Settings.System.getInt(context.getContentResolver(),

          Settings.System.AIRPLANE_MODE_ON, 0)  == 0;

32.監聽enter鍵,點擊enter實現登入或其他響應事件:

editText.setOnEditorActionListener(newOnEditorActionListener() {

 

@Override

publicboolean onEditorAction(TextView v, int actionId, KeyEvent event) {

// TODOAuto-generated method stub

Server.login(Login.this,name, pwd);

returnfalse;

}

});

33.若Activity設定全屏,則Fragment中setHasOptionsMenu(true);將看不到功能表項目。

34. PopupWindow:點擊外部表單時使其消失
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());//重要: 點擊使其消失,並且不會影響背景
35.用 SpannableStringBuilder處理TextView:
SpannableStringBuilder style=new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.argb(255, 12, 170, 218)),0,3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
mTextView.setText(style);
注意:設定自訂color時,優先調用Color.argb方法(使用Color.rgb可能會出錯)。

36.自訂Adapter繼承自BaseAdapter時,ViewHolder使用靜態類,非static的ViewHolder會導致ListView在滑動時比較卡。
37. java.lang.RuntimeException: Unable to destroy activity {com.demo.***.MainTabActivity}: java.lang.RuntimeException: Unable to destroy activity {com.demo.***.TabActivity01 }: java.lang.IllegalStateException: Activity has been destroyed
檢查Activity的生命週期,對於ondestroy不要放不相關的代碼。
38.ListView的item對某個widget進行觸摸點擊事件:
在item的布局中對相應的widget設定:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
主要考慮擷取焦點的優先順序,讓widget在預設情況下不擷取焦點。
39.EditText沒有邊框,聚焦時只有底部的邊框出現:

在manifest.xml中看android:targetSdkVersions是否已設定。例如:要在4.0的平台上測試,而android:targetSdkVersion=13(3.2平台),就會出現EditText缺失邊框的現象。刪除android:targetSdkVersion標籤即可。

 

40.對EditText,點擊螢幕,隱藏軟鍵盤:

對當前Activity的Layout設定id,然後重寫onclick方法:

InputMethodManagerimm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(v.getWindowToken(),0);

 

41. android.view.WindowManager$BadTokenException:Unable to add window -- tokenandroid.app.LocalActivityManager$LocalActivityRecord@41226b10 is not valid; isyour activity running?

在Activity或View中,顯示Dialog(AlertDialog),導致此錯誤。

1).如果構造Dialog使用的context是getApplication,改成相應的Activity,在Activity中進行添加view的操作。

2).如果使用了TabActivity(或者TabActivity裡面嵌套TabAcitivity),context使用getParent。

 

相關文章

聯繫我們

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