Android 改变相对高度

Android 改变相对高度,android,layout,android-studio,Android,Layout,Android Studio,我遇到了以下错误:java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams无法强制转换为android.widget.RelativeLayout$LayoutParams 我认为这个错误是因为我在线性布局中有一个相对布局 我寻找同样的问题: 但这没用 我的代码: LinearLayout.LayoutParams paramsT1score = (LinearLayout.LayoutParams)mT1layo

我遇到了以下错误:
java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams无法强制转换为android.widget.RelativeLayout$LayoutParams

我认为这个错误是因为我在线性布局中有一个相对布局 我寻找同样的问题:

但这没用

我的代码:

LinearLayout.LayoutParams paramsT1score = (LinearLayout.LayoutParams)mT1layoutScore.getLayoutParams();
                // Changes the height and width to the specified *pixels*
                paramsT1score.height = 80;
                mT1layout.setLayoutParams(paramsT1score);
这是我的.xml文件

</RelativeLayout>



<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="70dp"
        android:background="#314ebd"
        android:id="@+id/LayoutT1score"
        android:layout_toLeftOf="@+id/LayoutT2score">

        <TextView
            android:id="@+id/player1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 1"
            android:id="@+id/lblTeam1"
            android:layout_above="@+id/player1"
            android:layout_alignLeft="@+id/player1"
            android:layout_alignStart="@+id/player1"
            android:textSize="24dp"
            android:textColor="#ffffff" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="60dp"
        android:id="@+id/LayoutT2score"
        android:background="#fbff62"
        >

        <TextView
            android:id="@+id/player2"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="0dp"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 2"
            android:id="@+id/lblTeam2"
            android:textSize="24dp"
            android:textColor="#000000"
            android:layout_above="@+id/player2"
            android:layout_alignLeft="@+id/player2"
            android:layout_alignStart="@+id/player2" />


    </RelativeLayout>
</LinearLayout>


Hm,似乎MT1LayoutCore的参数是
LinearLayout.LayoutParams
,但mT2layout的参数是
RelativeLayout.LayoutParams

所以,我认为你应该创造

RelativeLayout.LayoutParams paramsT2score = (RelativeLayout.LayoutParams)mT2layout.getLayoutParams();
然后将所有需要的参数从paramsT1score复制到paramsT2score,然后添加

mT2layout.setLayoutParams(paramsT2score);

布局参数类型由父类型确定。您可以从一个视图
mT1layoutScore
中获取布局参数,然后尝试在另一个
mT2layout
中设置它们,其父视图的类型不同


在同一对象上设置布局参数,或者由于您正在在位编辑布局参数,只需调用
mT1layoutScore.requestLayout()

仍然收到相同的错误:android.widget.LinearLayout$LayoutParams无法强制转换为android.widget.RelativeLayout$LayoutParams在链接中提到您需要使用布局的父类型请参见链接:以编程方式设置RelativeLayout布局参数-抛出ClassCastException@matthijsW嗯,现在我想起来了。。。更改布局后,我出现了相同的错误。尝试清理和重建项目。我在代码中犯了一些愚蠢的错误,但谢谢你的帮助。有学问的something@matthijsW很高兴听到你发现了错误)你能给我一个广泛的例子吗?用
mT1layoutScore.requestLayout()
替换
mT2layout.setLayoutParams()
。谢谢,这是复制出错的经典例子。我想我需要多睡一会儿