[Android實例] android UI設計的一些心得與問題解決(無效果圖)

1.把
Button
或者
ImageButton
的背景設爲透明或者半透明
:
半透明<Buttonandroid:background="#e0000000" ... />
透明<Buttonandroid:background="#00000000".../>
其他透明度:在RGB的值前面加上數字(100以內),如#90436EEERGB436EEE),#50436EEE,根據值的大小呈現不同的顯示效果,值越小透明度越高。
2.設置按鈕背景圖片或顏色:
btn.setBackgroundResource(R.drawable.comment_sel);
btn.setTextColor(getResources().getColor(R.color.blue));
3.文字換行:;(要加上分號).
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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章