开发者

Android: set a RelativeLayout.LayoutParams, but got FrameLayout.LayoutParams later

I've built my custom ScrollView layout like below

public class MyScrollView extends ScrollView {

RelativeLayout mContentView = null;
public MyScrollView(final Context context) {
    super(context);

    RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(320, 480);
    adaptLayout.setMargins(0, 0, 0, 0);
    setLayoutParams(adaptLayout);

    mContentView = new RelativeLayout(context);
    RelativeLayout.LayoutParams adaptLayout2 = new RelativeLayout.LayoutParams(300, 960);
    adaptLayout2.setMargins(0, 0, 0, 0);
    mContentView.setLayoutParams(adaptLayout2);

    Button btn = new Button(context);
    btn.setText("start dialog");
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            RelativeLayout.LayoutParams params = (RelativeLayout.Layou开发者_JAVA百科tParams) mContentView.getLayoutParams();
            params.height = 1000;
            setLayoutParams(params);
            MyScrollView.this.invalidate();
        }

    });
}

However, I got FrameLayout.LayoutParams type later by using of getLayoutParams().

Really confusing behavior... Any idea? Thanks.

05-25 02:06:58.082: ERROR/AndroidRuntime(219): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams

05-25 02:06:58.082: ERROR/AndroidRuntime(219): at com.example.hello.MyScrollView$1.onClick(MyScrollView.java:42)

05-25 02:06:58.082: ERROR/AndroidRuntime(219): at android.view.View.performClick(View.java:2344)

05-25 02:06:58.082: ERROR/AndroidRuntime(219): at android.view.View.onTouchEvent(View.java:4133)


can you display the code when you get the layout Params ?? , i think you should cast your params to RelativeLayout.LayoutParams , do something like this :

adaptLAyout = (RelativeLayout.LayoutParams)myRelativeLayout.getLayoutParams();

EDIT : i see that you have a custom ScrollView, so can you tell me where you use it ? i think that you use it in a FrameLayout ?? try to use it in a RelativeLayout , that will fixe the problem , because when you get the LayoutParams , it referenced the parent Layout of you view , so if you use you scrollView in a FrameLAyout , you should works with FrameLAyout.LayoutParams , or you should use it in a RelativeLayout ,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜