[lottery] 08_购彩大厅(一)

Android 4.0

购彩大厅
问题1:在购物大厅的右边空白了一列
view.inflate第三个参数意义,root(inflate参数意义)
root:A view group that will be the parent. Used to properly inflate the layout_* parameters.
//一个ViewGroup作为父组件,通常用来正确的设置layout_* parameters参数信息


①分析:购彩大厅,使用View view = View.inflate(...,null)
导致到界面的时候,右边有一列白的
②原因:第三个参数为null
看源码可知道:为null的时候,view的LayoutParams的参数为null,
View result = root;
ViewGroup.LayoutParams params = null;
/*
问题:
如果root不为bull,才会设置LayoutParams参数的信息,那也就是说如果为null,
布局文件的android:layout_width和android:layout_height不起作用了,只会根据
里面显示的内容进行调整。
解决:手动设置LayoutParams的参数信息
*/
if (root != null) {
    if (DEBUG) {
        System.out.println("Creating params from root: " +
                root);
    }
    // Create layout params that match root, if supplied
    params = root.generateLayoutParams(attrs);
    if (!attachToRoot) {
        // Set the layout params for temp if we are not
        // attaching. (If we are, we use addView, below)
        temp.setLayoutParams(params);
    }
}
也就是说布局的layout_height和layout_width的参数无效
③解决:所以我们需要手动为view设置LayoutParams的参数
@Override
public View getView() {
    View view = View.inflate(context, R.layout.zl_hallnull);
    // 注意这里:第三个参数:root为null,setLayoutParams失效,需手动设置
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    view.setLayoutParams(params);
    return view;
}
问题2:用shape做背景资源(shape资源)
<?xml version="1.0" encoding="utf-8"?>
<!-- gradient   对应颜色渐变。 startcolor、endcolor就不多说了。 
     android:startColor 开始颜色
     android:endColor 结束颜色
     android:angle 是指从哪个角度开始变,上方是90°,逆时针方向的。
-->
<!-- solid      填充。 -->
<!-- stroke     描边。 -->
<!-- corners    圆角。 -->
<!-- padding    定义内容离边界的距离。 与android:padding_left、android:padding_right这些是一个道理。 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="30dp"/> //四个角的弧度
    <gradient
        android:angle="270"
        android:endColor="#0000ff"
        android:startColor="#FF0000" />
    <!-- 从270°(也就是垂直往下进行渐变的),开始颜色为红色到蓝色渐变的 -->
</shape> 
①问题:org.xmlpull.v1.XmlPullParserException: Binary XML file line #16<gradient> tag requires 'angle' attribute to be a multiple of 45

②解决:android:angle只能是45°的整数倍
android:angle角度问题:
startcolor:红色
endcolor:蓝色
方向:逆时针
→(0°),↑(90°),←(180°),↓(270°)
0°:

45°:

90°:

135°:

180°:

270°:


对于BaseView的子类中,有的需要设置监听,有的不需要,可以让BaseView实现OnClickListener
需要设置监听的子类,实现复写onclick()方法就可以了
代码的抽取:
1、init()和setListener()的抽取
2、将showView的参数设置抽取到父类
protected ViewGroup showView;  
/**
 * 获取到当前需要显示的内容
 * @return
 */
public View getView() {
    if (showView.getLayoutParams() == null) {// 只有为null的时候才需要设置LayoutParams
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        showView.setLayoutParams(params);
    }
    return showView;
}  
3、子类可以不用showView的抽取:findViewById(),直接findViewById就可以了
/**
     * 查找资源 
     * @param resId
     * @return
     */
    protected  View findViewById(int resId) {
        return showView.findViewById(resId);
    }