UI設計和實現(xiàn)是Android開發(fā)中必不可少的部分串结,UI做不好的話呻右,丑到爆跪妥,APP性能再好,估計也不會有多少人用吧声滥,而且如果UI和業(yè)務代碼邏輯中間沒有處理好眉撵,也會很影響APP的性能的。稍微總結(jié)一下落塑,開發(fā)中遇到的一些UI相關(guān)的問題纽疟,以及解決的方法,提供給有需要的人憾赁。
1污朽,Android全屏顯示
方法:requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
? ? ? ? ? ? ? ? WindowManager.LayoutParams.FLAG_FULLSCREEN);
2,visibility屬性VISIBLE龙考、INVISIBLE蟆肆、GONE的區(qū)別
設置方法:
XML文件:android:visibility="visible"
Java代碼:view.setVisibility(View.VISIBLE);
區(qū)別:
VISIBLE:設置控件可見。
INVISIBLE:設置控件不可見晦款。保留控件占有的布局空間炎功。
GONE:設置控件隱藏。不保留控件占有的布局空間缓溅。
3蛇损,創(chuàng)建帶圖標的右上角下拉菜單項(類似微信掃一掃菜單項)
/** *創(chuàng)建菜單 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST+1, 0, getResources().getString(R.string.scan_menu_text)).setIcon(R.drawable.scan_icon_24);
return true;
}
/**
* 利用反射機制調(diào)用MenuBuilder的setOptionalIconsVisible方法設置mOptionalIconsVisible為true,給菜單設置圖標時才可見
* 讓菜單同時顯示圖標和文字
* @param featureId
* @param menu
* @return
*/
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
method.setAccessible(true);
method.invoke(menu, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onMenuOpened(featureId, menu);
}
/**
*菜單的點擊事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item);
switch (item.getItemId()){
case Menu.FIRST+1:
Intent intent = new Intent(RIWarehouseActivity.this, CaptureActivity.class); startActivityForResult(intent, Constant.REQ_QR_CODE);
break;
default: break;
}
return true;
}
4,給控件添加邊框樣式
在drawable目錄添加border_style.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
? ?<stroke android:width="1dp" android:color="#999999"/>
????<solid android:color="@android:color/transparent"/>
????<corners android:radius="2dp"></corners>
</shape>
在控件的布局屬性中加上android:background="@drawable/border_style"就可以啦淤齐。
5股囊,修改Spinner默認樣式
創(chuàng)建custom_spinner_item_layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!--spinner展開后的Item布局-->
<TextView android:id="@+id/spinner_textView" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="3dp" android:textSize="8pt" android:layout_margin="5dp" android:textColor="@color/colorSimpleBlack"></TextView>
activity中使用:
arrAdapter = new ArrayAdapter<String>(ScanFirstActivity.this, R.layout.custom_spinner_item_layout, storageDeptsList);storageDeptSpinner.setAdapter(arrAdapter);
6,TextView加下劃線效果
資源文件里:
<resources>
<string name="hello"><u>phone:0123456</u></string>
?</resources>?
代碼里:
TextView textView = (TextView)findViewById(R.id.tv_test); textView.setText(Html.fromHtml("<u>"+"0123456"+"</u>"));
或者
textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下劃線? ? ? textView? .getPaint().setAntiAlias(true);//抗鋸齒?
7更啄,切換到中英文之后稚疹,頁面白板的問題
原因:文字過長,超出頁面锈死,導致整個頁面無法刷新出來贫堰。修改文字長度就可以。
8待牵,頁面整體滾動以及出現(xiàn)的問題
方法:在布局外面上加上ScrollView。使其適應父控件喇勋,即填滿屏幕缨该。當里面的內(nèi)容不超過一屏的時候,不可滾動川背;當內(nèi)容超過一屏的時候贰拿,可以滾動。
會出現(xiàn)的問題是:ScrollView can host only one direct child熄云。
原因:ScrollView 只能有一個直接子布局膨更。
解決方法:講多個子布局包裹在一個布局里面,再把這個布局作為ScrollView的子布局缴允。
9荚守,Dialog Unable to add window -- token null is not for an application錯誤的解決方法
AlertDialog dialog = new AlertDialog.Builder(context, R.style.Dialog).create();
參數(shù)context不能使用全局的application,必須使用當前activity的context练般。
10矗漾,Android自定義View之PopupLayout
歡迎使用自定義彈出框PopupLayout?
使用的時候,基于這個彈出框薄料,在上面布局:
代碼里使用:
View view=View.inflate(LoginActivity.this,R.layout.popup_fill_host_layout,null);
popupLayout=PopupLayout.init(LoginActivity.this,view);
popupLayout.setUseRadius(true);popupLayout.setWidth(350,true);
popupLayout.setHeight(300,true);
popupLayout.show(PopupLayout.POSITION_CENTER);
//綁定控件
hostPortEdit = view.findViewById(R.id.hostPortEdit);
hostPortEdit.setText(PreferenceUtil.getPreference(getApplication(),"Host"));
hostFillConfirmBtn = view.findViewById(R.id.hostFillConfirmBtn);
hostFillGiveUpBtn = view.findViewById(R.id.hostFillGiveUpBtn);
hostFillConfirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newHost = hostPortEdit.getText().toString();
if(!StringUtil.isBlank(newHost)){ PreferenceUtil.setPreference(getApplication(),"Host",newHost);}
Toast.makeText(getApplicationContext(), getResources().getString(R.string.save_success), Toast.LENGTH_SHORT).show(); popupLayout.dismiss();
}});
hostFillGiveUpBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
????popupLayout.dismiss();
}});
11敞贡,獲取自定義view中的控件
View view=View.inflate(LoginActivity.this,R.layout.popup_fill_host_layout,null);
popupLayout=PopupLayout.init(LoginActivity.this,view);
popupLayout.setUseRadius(true);
popupLayout.setWidth(350,true);
popupLayout.setHeight(300,true);
popupLayout.show(PopupLayout.POSITION_CENTER);
//綁定控件
hostPortEdit = view.findViewById(R.id.hostPortEdit);
或者
PopUpDialog newDialog = new PopUpDialog(MsgReView.this, R.style.MyDialog); newDialog.setCanceledOnTouchOutside(true);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? View view = newDialog.getCustomView();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TextView text1 = (TextView)view.findViewById(R.id.textViewTotal);
12,集成二維碼掃描功能
歡迎使用:封裝與接入ZXing掃碼庫
碼字不易摄职,如果覺得有幫助誊役,一定要給我點贊喲~~
不然信不信我砸了你家燈,半夜偷親你 ( ̄ε  ̄) !!!