Android:设置一个RelativeLayout.LayoutParams,但稍后得到了FrameLayout.LayoutParams

时间:2011-05-24 08:54:27

标签: android view android-layout

我已经构建了我的自定义ScrollView布局,如下所示

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.LayoutParams) mContentView.getLayoutParams();
            params.height = 1000;
            setLayoutParams(params);
            MyScrollView.this.invalidate();
        }

    });
}

但是,我稍后使用getLayoutParams()获得FrameLayout.LayoutParams类型。
真是令人困惑的行为......有什么想法吗?感谢。

  

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):在android.view.View.performClick(View.java:2344)
  05-25 02:06:58.082:ERROR / AndroidRuntime(219):在android.view.View.onTouchEvent(View.java:4133)

1 个答案:

答案 0 :(得分:0)

你可以在获得布局时显示代码吗? ,我认为你应该把你的参数投射到 RelativeLayout.LayoutParams ,做这样的事情:

adaptLAyout = (RelativeLayout.LayoutParams)myRelativeLayout.getLayoutParams();
编辑:我看到你有一个自定义的ScrollView,所以你能告诉我你在哪里使用它吗?我认为你在FrameLayout中使用它?尝试在RelativeLayout中使用它,这将解决问题,因为当你获得LayoutParams时,它引用了你查看的父布局,所以如果你在FrameLAyout中使用scrollView,你应该使用FrameLAyout.LayoutParams,或者你应该在RelativeLayout中使用它,