zoukankan      html  css  js  c++  java
  • android遇到的几个问题

    1.fragment的layout中,把imageview的layout_width错写成width,layout_height错写成height,导致Binary XML file line #16: Error inflating class fragment。

         对于每个组件,可以独自的使用Layout _width ,layout_heigh直接的设置相对于父容器的大小,设置为 wrap_content或者 match_parent. 但是heigh ,width不能设置这样设置,不能设置相对于父容器,否则会产生error: Error: String types not allowed (at 'width' with value 'wrap_content')。

         并且,一个组件可以只有Layout _width ,layout_height。但却不能只有heigh ,width,而没有Layout _width ,layout_height,因为那样的组件会看不到。如果你要使用heigh ,width的话,就要先设置Layout _width ,layout_height,把heigh ,width用来作为组件的微调使用。

    2.继承listfragment的布局文件中放入listview,那么它的id是@id/list而不是@+id/***。

    3.让多个Fragment 切换时不重新实例化(http://www.yrom.net/blog/2013/03/10/fragment-switch-not-restart/)

    在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:
    public void switchContent(Fragment fragment) {
    if(mContent != fragment) {
    mContent = fragment;
    mFragmentMan.beginTransaction()
    .setCustomAnimations(android.R.anim.fade_in, R.anim.slide_out)
    .replace(R.id.content_frame, fragment) // 替换Fragment,实现切换
    .commit();
    }
    }
    但是,这样会有一个问题:
    每次切换的时候,Fragment都会重新实例化,重新加载一边数据,这样非常消耗性能和用户的数据流量。
    就想,如何让多个Fragment彼此切换时不重新实例化?
    翻看了Android官方Doc,和一些组件的源代码,发现,replace()这个方法只是在上一个Fragment不再需要时采用的简便方法。
    正确的切换方式是add(),切换时hide(),add()另一个Fragment;再次切换时,只需hide()当前,show()另一个。
    这样就能做到多个Fragment切换不重新实例化:
    public void switchContent(Fragment from, Fragment to) {
    if (mContent != to) {
    mContent = to;
    FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(
    android.R.anim.fade_in, R.anim.slide_out);
    if (!to.isAdded()) { // 先判断是否被add过
    transaction.hide(from).add(R.id.content_frame, to).commit(); // 隐藏当前的fragment,add下一个到Activity中
    } else {
    transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
    }
    }
    }

    4.多个fragment用findViewById()函数进行获取指定的fragment,竟然返回null。找了好久,终于发现问题所在:我给findViewById传的是layout id,而参数需要的是activity中的fragment id,修改后,就ok了。

    5.自定义button点击效果的selector的item顺序有要求,(貌似是state_pressed="true",state_focused="true",state_focused="false",否则,点击效果可能显示不出来。)看了官方文档才知道,默认显示的要放在状态列表的最后,否则不会有状态图片切换。

  • 相关阅读:
    Tomcat架构解析(五)-----Tomcat的类加载机制
    session与cookie
    freemarker常用标签解释遍历
    freemarker常用标签解释三
    freemarker常用标签解释二
    freemarker常用标签解释
    禁止浏览器自动填充
    使用cookie实现自动登录
    长连接和短连接
    filter防止xxs攻击
  • 原文地址:https://www.cnblogs.com/playerboy/p/4105935.html
Copyright © 2011-2022 走看看