Idea在RelativeLayout上的Touch侦听器的帮助下改变了自身的大小(宽度和高度)。 RelativeLayout里面的子节点是一个GridLayout,它本身由多个FrameLayouts组成(我猜这是不相关的)。 GridLayout的宽度和高度设置为match_parent(如RelativeLayout)。理论上,GridLayout应该根据运行时的父级进行更改。
但每当我尝试更改RelativeLayout的参数时,我都会收到以下错误消息:
android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.constraint.ConstraintLayout $ LayoutParams
以下是XML布局的相关部分
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/maingrid">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="4"
android:id="@+id/grid">
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
这是代码
private RelativeLayout Wall = findViewById(R.id.maingrid);
Wall.setLayoutParams(new RelativeLayout.LayoutParams(newWidth,newHeight));
我试图将RelativeLayout.LayoutParams更改为其他尝试解决异常错误的类。但我只是得到了其他类似的异常错误。想想我可能会遗漏一些东西。
是否可以使用上面的XML Layout结构并在运行时生成所需的行为
被修改
也许我应该提到带有GridLayout的XML文件包含在
下面的主要布局中<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.lucy.pixelli.MainActivity"
tools:showIn="@layout/activity_main">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<include layout="@layout/grid" />
这是完整错误消息
FATAL EXCEPTION: main
Process: com.example.lucy.pixelli, PID: 11119
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams
at android.support.constraint.ConstraintLayout.getViewWidget(ConstraintLayout.java:877)
at android.support.constraint.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:643)
at android.support.constraint.ConstraintLayout.updateHierarchy(ConstraintLayout.java:631)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:971)
at android.view.View.measure(View.java:19978)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6127)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19978)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6127)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786)
at android.view.View.measure(View.java:19978)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6127)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19978)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6127)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19978)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6127)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19978)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6127)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19978)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6127)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:694)
at android.view.View.measure(View.java:19978)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2405)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1457)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1720)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1333)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6675)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:887)
at android.view.Choreographer.doCallbacks(Choreographer.java:699)
at android.view.Choreographer.doFrame(Choreographer.java:634)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:873)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
答案 0 :(得分:0)
您想将LayoutParams
设置为RelativeLayout
。
LayoutParams
应该是该视图的父类型。
由于您的RelativeLayout
包含在ConstraintLayout
中,因此您的LayoutParams
应为ConstraintLayout.LayoutParam
。
对于只有LayoutParams
的{{1}},您只需使用width/height