layout01 = (LinearLayout)findViewById(R.id.screen01);
        LayoutParams params =  layout01.getLayoutParams();
        params.width = 200;
        params.height = 200;
        layout01.setBackgroundResource(R.drawable.mylistempty);
        
        layout01.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
        LayoutParams params =  layout01.getLayoutParams();
        params.width = 320;
        params.height = 400;
        layout01.setLayoutParams(params);
                          layout01.setBackgroundResource(R.drawable.mylistempty);         layout01.invalidate();
         }
});有人知道为什么我点击后这个layout的大小没有变呢??想实现一个layout开始为200*200,点击后变成320*400,其背景为根据layout大小来变。急啊。。有人知道么?

解决方案 »

  1.   

       LayoutParams mParams;//搞成全局变量
       layout01 = (LinearLayout)findViewById(R.id.screen01);
       mParams = layout01.getLayoutParams();
       mParams.width = 200;
       mParams.height = 200;
       layout01.setLayoutParams(mParams);
       layout01.setBackgroundResource(R.drawable.mylistempty);
         
      layout01.setOnClickListener(new OnClickListener() { @Override
     public void onClick(View v) {
     mParams.width = 320;
     mParams.height = 400;
     layout01.setLayoutParams(mParams);
     layout01.setBackgroundResource(R.drawable.mylistempty);
     }
     });
      

  2.   

    我试过了可以,但是我的背景用的是颜色尔不是图片,或者准备2张尺寸分别为200*200和320*400的图片,还可以把图片处理成9.png各式的,要是这些方法都不行,把你的完整的代码全发上来
       LayoutParams mParams;//搞成全局变量
       layout01 = (LinearLayout)findViewById(R.id.screen01);
       layout01.setBackgroundColor(Color.RED);
       mParams = layout01.getLayoutParams();
       mParams.width = 200;
       mParams.height = 200;
       layout01.setLayoutParams(mParams);
         
      layout01.setOnClickListener(new OnClickListener() {  @Override
      public void onClick(View v) {
      mParams.width = 320;
      mParams.height = 400;
      layout01.setLayoutParams(mParams);
      }
      });
      

  3.   

    其实我需要是要实现一个横向滚动,然后每个页面内容可以放大缩小,有什么建议么?或者提供点代码。。
    我用的launch。但是里面实现不能 下面是xml<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout007"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/woodmylistbg">
    <com.yao_guet.test.ScrollLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollLayoutTest"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <FrameLayout android:layout_width="fill_parent" android:id="@+id/frame01"
    android:layout_height="fill_parent" android:layout_gravity="center">
    <include layout="@layout/list_screen"></include>
    </FrameLayout>
    <!--
    <FrameLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_gravity="center">
    <LinearLayout android:layout_width="wrap_content"
    android:id="@+id/screen01" android:layout_height="wrap_content"
    android:gravity="center"> <ImageView android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /> </LinearLayout>
    </FrameLayout>
    -->
    <FrameLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_gravity="center">
    <LinearLayout android:id="@+id/screen02"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/addtocart_sel">
    </LinearLayout>
    </FrameLayout>
    <FrameLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_gravity="center">
    <LinearLayout android:id="@+id/screen03"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/navbarbg">
    </LinearLayout>
    </FrameLayout>
    <!--
    <LinearLayout android:background="#FF00"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"> <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Button1" />
    </LinearLayout> <LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"> <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Button2" />
    </LinearLayout>
    -->
    </com.yao_guet.test.ScrollLayout>
    </LinearLayout>
      

  4.   

    package com.yao_guet.test;import android.content.Context;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.VelocityTracker;
    import android.view.View;
    import android.view.ViewConfiguration;
    import android.view.ViewGroup;
    import android.widget.Scroller;/**
     * 仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类
     * @author Yao.GUET
     * blog: http://blog.csdn.net/Yao_GUET
     * date: 2011-05-04
     */
    public class ScrollLayout extends ViewGroup { private static final String TAG = "ScrollLayout";
    private Scroller mScroller;
    private VelocityTracker mVelocityTracker;

    private int mCurScreen;
    private int mDefaultScreen = 0;

    private static final int TOUCH_STATE_REST = 0;
    private static final int TOUCH_STATE_SCROLLING = 1;

    private static final int SNAP_VELOCITY = 600;

    private int mTouchState = TOUCH_STATE_REST;
    private int mTouchSlop;
    private float mLastMotionX;
    private float mLastMotionY;

    private int offsetView = 0; //LLYY ADD public ScrollLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    // TODO Auto-generated constructor stub
    } public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    mScroller = new Scroller(context);

    mCurScreen = mDefaultScreen;
    mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    } @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO Auto-generated method stub
    if (changed) {
    int childLeft = offsetView;
    final int childCount = getChildCount();

    for (int i=0; i<childCount; i++) {
    final View childView = getChildAt(i);
    if (childView.getVisibility() != View.GONE) {
    final int childWidth = childView.getMeasuredWidth() - offsetView*2;
    childView.layout(childLeft, 0, 
    childLeft+childWidth, childView.getMeasuredHeight());
    childLeft += childWidth;
    }
    }
    }
    }
        @Override  
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   
         Log.e(TAG, "onMeasure");
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);   
      
            final int width = MeasureSpec.getSize(widthMeasureSpec);   
            final int widthMode = MeasureSpec.getMode(widthMeasureSpec);   
            if (widthMode != MeasureSpec.EXACTLY) {   
                throw new IllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!"); 
            }   
      
            final int heightMode = MeasureSpec.getMode(heightMeasureSpec);   
            if (heightMode != MeasureSpec.EXACTLY) {   
                throw new IllegalStateException("ScrollLayout only can run at EXACTLY mode!");
            }   
      
            // The children are given the same width and height as the scrollLayout   
            final int count = getChildCount();   
            for (int i = 0; i < count; i++) {   
                getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);   
            }   
            // Log.e(TAG, "moving to screen "+mCurScreen);   
            scrollTo(mCurScreen * width, 0);         
        }  
        
        /**
         * According to the position of current layout
         * scroll to the destination page.
         */
        public void snapToDestination() {
         final int screenWidth = getWidth();
         final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;
         snapToScreen(destScreen);
        }
        
        public void snapToScreen(int whichScreen) {
         // get the valid layout page
         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
         if (getScrollX() != (whichScreen*getWidth())) {
        
         final int delta = whichScreen*getWidth()-getScrollX();
         mScroller.startScroll(getScrollX(), 0, 
         delta, 0, Math.abs(delta)*2);
         mCurScreen = whichScreen;
         invalidate(); // Redraw the layout
         }
        }
        
        public void setToScreen(int whichScreen) {
         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
         mCurScreen = whichScreen;
         scrollTo(whichScreen*getWidth(), 0);
        }
        
        public int getCurScreen() {
         return mCurScreen;
        }
        
    @Override
    public void computeScroll() {
    // TODO Auto-generated method stub
    if (mScroller.computeScrollOffset()) {
    scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
    postInvalidate();
    }
    } @Override
    public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    if (mVelocityTracker == null) {
    mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);

    final int action = event.getAction();
    final float x = event.getX();
    final float y = event.getY();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
    Log.e(TAG, "event down!");
    if (!mScroller.isFinished()){
    mScroller.abortAnimation();
    }
    mLastMotionX = x;
    break;

    case MotionEvent.ACTION_MOVE:
    int deltaX = (int)(mLastMotionX - x);
    mLastMotionX = x;

                scrollBy(deltaX, 0);
    break;

    case MotionEvent.ACTION_UP:
    Log.e(TAG, "event : up");   
                // if (mTouchState == TOUCH_STATE_SCROLLING) {   
                final VelocityTracker velocityTracker = mVelocityTracker;   
                velocityTracker.computeCurrentVelocity(1000);   
                int velocityX = (int) velocityTracker.getXVelocity();               Log.e(TAG, "velocityX:"+velocityX); 
                
                if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {   
                    // Fling enough to move left   
                 Log.e(TAG, "snap left");
                    snapToScreen(mCurScreen - 1);   
                } else if (velocityX < -SNAP_VELOCITY   
                        && mCurScreen < getChildCount() - 1) {   
                    // Fling enough to move right   
                 Log.e(TAG, "snap right");
                    snapToScreen(mCurScreen + 1);   
                } else {   
                    snapToDestination();   
                }               if (mVelocityTracker != null) {   
                    mVelocityTracker.recycle();   
                    mVelocityTracker = null;   
                }   
                // }   
                mTouchState = TOUCH_STATE_REST;   
    break;
    case MotionEvent.ACTION_CANCEL:
    mTouchState = TOUCH_STATE_REST;
    break;
    }

    return true;
    } @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    Log.e(TAG, "onInterceptTouchEvent-slop:"+mTouchSlop);

    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && 
    (mTouchState != TOUCH_STATE_REST)) {
    return true;
    }

    final float x = ev.getX();
    final float y = ev.getY();

    switch (action) {
    case MotionEvent.ACTION_MOVE:
    final int xDiff = (int)Math.abs(mLastMotionX-x);
    if (xDiff>mTouchSlop) {
    mTouchState = TOUCH_STATE_SCROLLING;

    }
    break;

    case MotionEvent.ACTION_DOWN:
    mLastMotionX = x;
    mLastMotionY = y;
    mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
    break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
    mTouchState = TOUCH_STATE_REST;
    break;
    }

    return mTouchState != TOUCH_STATE_REST;
    }

    }
      

  5.   

    package com.yao_guet.test;import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.ColorFilter;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Bitmap.Config;
    import android.graphics.Paint.Style;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;public class ScrollLayoutTest extends Activity {
    private String screenTitle [] = {"aap.com","bb.com","cc.com","sdd"};
    // private ImageView image1,image2,image3; private LinearLayout layout01,layout007;
    private LayoutParams params;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
    //        layout007 = (LinearLayout)findViewById(R.id.layout007);//        layout01 = (LinearLayout)findViewById(R.id.screen01);        params = layout01.getLayoutParams();
            params.width = 200;
            params.height = 200;
            layout01.setLayoutParams(params);
    //        image1.setImageResource(R.drawable.emptymylistmessage);
            layout01.setBackgroundResource(R.drawable.emptymylistmessage);
            
            layout01.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
            params.width = 320;
            params.height = 400;
            layout01.setLayoutParams(params);
    //         image1.setImageResource(R.drawable.mylistempty);
            layout01.setBackgroundResource(R.drawable.emptymylistmessage);
    }
    });
          
        }    public Drawable getMyImage(String title,int width,int height, boolean normal)
        {
         Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

         Bitmap bitmap1 = BitmapFactory.decodeResource(this.getResources(), R.drawable.navbarbg);
    Bitmap bitmap2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.mylistempty);

    canvas.drawBitmap(bitmap1, 0, 0, null);
    canvas.drawBitmap(bitmap2, 0, bitmap1.getHeight(), null);

    Paint paint = new Paint();
    paint.setColor(0xFF000000);
    paint.setStyle(Style.STROKE);
    canvas.drawText(title, width/2, bitmap1.getHeight(), paint); Drawable able = null;
    // if(!normal)
    // {
    // Matrix m = new Matrix();
    // int w = width * 1/2;
    // int h = height * 1/2;
    // m.postScale(w, h);
    //
    // Bitmap bb = Bitmap.createScaledBitmap(bitmap, w, h,false);
    // able = new BitmapDrawable(bb);
    // }else{
    able = new BitmapDrawable(bitmap);
    // }     return able;
        }
    }
      

  6.   

    麻烦lz把整个工程发给我,qq:394888075