【Android】近日问题小计

1.Android重写FragmentTabHost来实现状态保存
关键代码:关键是使用hide与show方法,这样fragment在不可见时并不会被destroyView

if (mLastTab != newTab) {  
            if (ft == null) {  
                ft = mFragmentManager.beginTransaction();  
            }  
            if (mLastTab != null) {  
                if (mLastTab.fragment != null) {  
//                  ft.detach(mLastTab.fragment);  
                    ft.hide(mLastTab.fragment);  
                }  
            }  
            if (newTab != null) {  
                if (newTab.fragment == null) {  
                    newTab.fragment = Fragment.instantiate(mContext,  
                            newTab.clss.getName(), newTab.args);  
                    ft.add(mContainerId, newTab.fragment, newTab.tag);  
                } else {  
//                  ft.attach(newTab.fragment);  
                    ft.show(newTab.fragment);  
                }  
            }  

            mLastTab = newTab;  
        }  

2.在重写View时,分发点击事件,其实也可以在其中进行event的down、up等判断。Android触摸事件分发机制;
另外:ontouch 事件传递机制

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }

3.How to Programmatically Add Views to Views

LinearLayout myLayout = findViewById(R.id.main);

Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.FILL_PARENT,
                                     LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton);

4.How to force an entire layout View refresh?强制重绘

ViewGroup vg = findViewById (R.id.mainLayout);
vg.invalidate();

Invalidate the whole view. If the view is visible, onDraw(Canvas) will
be called at some point in the future. This must be called from a UI
thread. To call from a non-UI thread, call postInvalidate().

5.checkBox 边距、文字图片间距的各种问题
常规xml做法on stackOverFlow
我最后是自己重画了一个View,模拟了checkbox,实现了上图下字的样式:

@Override
    protected void onDraw(Canvas canvas) {
        int height = getHeight();
        int width = getWidth();

        int bipXY = (int) (40.0 / 95 * height);

        Bitmap bitmap = picture;
        bitmap = BitmapU.processBitmap(bitmap, bipXY, bipXY);

        float drawX = (float) (85.0 / 340 * width);
        TAB = TAB_MOMENT;
        if (side == MALL) {
            drawX = (float) (215.0 / 340 * width);
            TAB = TAB_MALL;
        }

        float drawY = (float) (18.0 / 95 * height);

        canvas.drawBitmap(bitmap, drawX, drawY, null);
        Paint paint = new Paint();

        paint.setColor(textColor);                          //设置画笔颜色
        Typeface tf = FontU.getInstance(mContext).getCustomFont();
        paint.setTypeface(tf);             //设置字体样式
        paint.setStrokeWidth(8);

        //设置画笔字体的大小
        paint.setTextSize((float) (20.0 / 95 * height));
        canvas.drawText(TAB, drawX + (float) (1.0 / 340 * width), height - (float) (16.0 / 95 * height), paint);

        super.onDraw(canvas);
    }

这里附上一个重绘checkbox为对勾的canvas绘制方法
canvas绘图,基础教程
6.RelativeLayout底部居中:

android:layout_alignParentBottom="true"

7.HTTP的PUT、DELETE请求类型操作方法(SOF)
8.NetworkOnMainThreadException
http操作不能在主线程中进行;

 new Thread(new Runnable() {
     @Override
     public void run() {
     }
 }).start();// 错在在这里直接run()

你可能感兴趣的:(Android问题记录)