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

发布于 2024-11-09 05:06:53 字数 1593 浏览 0 评论 0原文

我已经构建了如下所示的自定义 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: 错误/AndroidRuntime(219): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
05-25 02:06:58.082: 错误/AndroidRuntime(219): 在 com.example.hello.MyScrollView$1.onClick(MyScrollView.java:42)
05-25 02:06:58.082: 错误/AndroidRuntime(219): 在 android.view.View.performClick(View.java:2344)
05-25 02:06:58.082: 错误/AndroidRuntime(219): 在 android.view.View.onTouchEvent(View.java:4133)

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.LayoutParams) 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-11-16 05:06:53

当你得到布局参数时你能显示代码吗? ,我认为你应该将你的参数转换为RelativeLayout.LayoutParams,做这样的事情:

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

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

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 ,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文