android 开发问题总结

54 阅读3分钟

怎么使RelativeLayout内的布局竖直居中? layout_centerVertical="true"?不理想,原因是 如果RelativeLayout使用的是9-patch图作为background,无法保证在9-patch内标注的内容居中,这个属性只是保证在整个RelativeLayout布局内居中。

在RelativeLayout 中设置 setGravity 或 android:gravity 属性可以保证内容居中。

是不是layout_gravity="center_vertical"可以保证在9-patch图内容居中呢? 结论是,NO。 layout_gravity 似乎不被RelativeLayout所支持,具有原因待确定。

另外,gravity 和 layout_centerVertical 冲突, 不要同时设置。

问题? setGravity 仅当 RelativeLayout 布局内含有一个child view 时, 才有效, 原因不明. 猜测: setGravity是当所有的child views 布局完成后, 把child views作为一个单元view来设置它在parent中的gravity。

  • setDuplicateParentStateEnabled not work? 如果view同时设置了android:clickable=true, setDuplicateParentStateEnabled as true will not work.

  • 避免横竖屏幕导致activity重建生命周期的方法,在activity的声明中加入:

android:configChanges="orientation|keyboardHidden|screenSize"
  • 如何动态更新actionbar右端更多(三个点)的显示/隐藏:
    private void updateActionbarMenu() {
        mHandler.postDelayed(new Runnable() {
            public void run() {
                mActivity.this.invalidateOptionsMenu();
            }
        }, 100); //需要延迟100m,否则会出现不生效的问题
    }
  • 默认的PreferenceManager.getDefaultSharedPreferences(context)在多进程同时读写时,容易被清空 很多程序员为了方便,通常使用默认的sharedPref,事实上,其具有一定的局限性,默认的sharedPref仅支持读写模式为Context.MODE_PRIVATE,无法支持Context.MODE_MULTI_PROCESS,不支持到不重要,坑的是,当安卓程序有多个进程同时读写时,会导致默认的sharedPref内容被清空。因此,不推荐大家使用默认的sharedPref,虽然你当前程序是单进程的,不保证后续版本会不会新开一个进程运行其他组件或插件。建议使用如下方式存放配置:
    SharedPreferences pref = context.getSharedPreferences(
                SHAREPREFERENCE_NAME, Context.MODE_PRIVATE |Context.MODE_MULTI_PROCESS);
  • 下载云盘文件失败的原因 通常只需给定一个下载url既可以下载文件,然后如果复制云盘下载url在别的地方下载则无法下载成功,返回404.原因是在执行下载的httpGet的头部要加入cookie,实现方法如下:
    private void addCookie(HttpGet httpGet) {
        CookieSyncManager.createInstance(mContext);
        String cookie = CookieManager.getInstance().getCookie(downloadUrl);
        if (!TextUtils.isEmpty(cookie)) {
            httpGet.setHeader("Cookie", cookie);
        }
    }

the mean of "?"

引用的作用, 比如下面的?引用上面的windowFrame属性的值。 <style parent="@android:style/Theme" name="CustomTheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowFrame">@drawable/icon</item> <item name="android:windowBackground">?android:windowFrame</item> </style>

##custom attr 在res/values 文件下定义一个attrs.xml 文件 `

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="MyView">  
        <attr name="textColor" format="color" />  
        <attr name="textSize" format="dimension" />  
        <attr name="Oriental">
            <enum name="Horizontal" value="1"></enum>
            <enum name="Vertical" value="0"></enum>
        </attr>

    </declare-styleable>  
</resources>  

`

  • use in code

`

public MyView(Context context, AttributeSet attrs)  
    {  
        super(context,attrs);  
        mPaint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,  
                R.styleable.MyView);  
          
        int textColor = a.getColor(R.styleable.MyView_textColor,  
                0XFFFFFFFF);  
        float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
          
        mPaint.setTextSize(textSize);  
        mPaint.setColor(textColor);  
          
        a.recycle();  
    }  

` R.sytleable.MyView_textColor: MyView 就是定义在 里的 名字; 获取里面属性用 名字_ 属性 连接起来就可以. TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!

自定义属性attr的format,可以有以下多种:

reference string color dimension boolean integer float fraction enum flag

  • use in xml

`

<?xml   
version="1.0" encoding="utf-8"?>  
<LinearLayout   
xmlns:android="http://schemas.android.com/apk/res/android"  
                
xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="@string/hello"  
    />  
<com.android.tutor.MyView  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    test:textSize="20px"  
    test:textColor="#fff"  
/>  
</LinearLayout>  

`

" xmlns:test ="schemas.android.com/apk/res/com…" ,test是自定义属性的前缀, com.android.tutor 是我们包名.

references: blog.csdn.net/jincf2011/a… blog.csdn.net/wanjf_912/a… www.cnblogs.com/crashmaker/… www.vogella.com/tutorials/A… www.vogella.com/tutorials/A…