Android:设置了一个RelativeLayout.LayoutParams,但后来得到了FrameLayout.LayoutParams
我已经构建了如下所示的自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你得到布局参数时你能显示代码吗? ,我认为你应该将你的参数转换为RelativeLayout.LayoutParams,做这样的事情:
编辑:我看到你有一个自定义的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 :
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 ,