Android開發(fā)中UI相關(guān)的問題總結(jié)

UI設(shè)計和實現(xiàn)是Android開發(fā)中必不可少的部分,UI做不好的話,丑到爆,APP性能再好,估計也不會有多少人用吧,而且如果UI和業(yè)務(wù)代碼邏輯中間沒有處理好,也會很影響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ū)別

設(shè)置方法:

XML文件:android:visibility="visible"

Java代碼:view.setVisibility(View.VISIBLE);

區(qū)別:

VISIBLE:設(shè)置控件可見。

INVISIBLE:設(shè)置控件不可見。保留控件占有的布局空間。

GONE:設(shè)置控件隱藏。不保留控件占有的布局空間。

3,創(chuàng)建帶圖標(biāo)的右上角下拉菜單項(類似微信掃一掃菜單項)

/** *創(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;

}

/**

* 利用反射機(jī)制調(diào)用MenuBuilder的setOptionalIconsVisible方法設(shè)置mOptionalIconsVisible為true,給菜單設(shè)置圖標(biāo)時才可見

* 讓菜單同時顯示圖標(biāo)和文字

* @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默認(rèn)樣式

創(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,切換到中英文之后,頁面白板的問題

原因:文字過長,超出頁面,導(dǎo)致整個頁面無法刷新出來。修改文字長度就可以。

8,頁面整體滾動以及出現(xiàn)的問題

方法:在布局外面上加上ScrollView。使其適應(yīng)父控件,即填滿屏幕。當(dāng)里面的內(nèi)容不超過一屏的時候,不可滾動;當(dāng)內(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,必須使用當(dāng)前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掃碼庫


碼字不易,如果覺得有幫助,一定要給我點贊喲~~

不然信不信我砸了你家燈,半夜偷親你 ( ̄ε  ̄) !!!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容