zoukankan      html  css  js  c++  java
  • Android开发中UI相关的问题总结

    UI设计和实现是Android开发中必不可少的部分,UI做不好的话,丑到爆,APP性能再好,估计也不会有多少人用吧,而且如果UI和业务代码逻辑中间没有处理好,也会很影响APP的性能的。稍微总结一下,开发中遇到的一些UI相关的问题,以及解决的方法,提供给有需要的人。

    1,Android全屏显示

    方法:requestWindowFeature(Window.FEATURE_NO_TITLE);

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

                    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    2,visibility属性VISIBLE、INVISIBLE、GONE的区别

    设置方法:

    XML文件:android:visibility="visible"

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

    区别:

    VISIBLE:设置控件可见。

    INVISIBLE:设置控件不可见。保留控件占有的布局空间。

    GONE:设置控件隐藏。不保留控件占有的布局空间。

    3,创建带图标的右上角下拉菜单项(类似微信扫一扫菜单项)

    /** *创建菜单 */

    @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;

    }

    /**

    * 利用反射机制调用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默认样式

    创建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,页面整体滚动以及出现的问题

    方法:在布局外面上加上ScrollView。使其适应父控件,即填满屏幕。当里面的内容不超过一屏的时候,不可滚动;当内容超过一屏的时候,可以滚动。

    会出现的问题是: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();

    参数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扫码库

    码字不易,如果觉得有帮助,一定要给我点赞哟~~

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

  • 相关阅读:
    codeforces 1249D1/D2 Too Many Segments (贪心)
    codeforces 1244E Minimizing Difference (贪心)
    codeforces 1249C1 + 1249C2 (贪心)
    codeforces 1251D Salary Changing (二分+贪心)
    codeforces 1244C (思维 or 扩展欧几里得)
    P1040 加分二叉树(区间DP)
    POJ 1015 Jury Compromise (记录路径的背包问题)
    混合背包(单调队列优化多重背包)(模板)
    GAMES101系列笔记一 图形学概述与线性代数入门
    力扣514 自由之路
  • 原文地址:https://www.cnblogs.com/tonyccc/p/11470098.html
Copyright © 2011-2022 走看看