改变相对布局高度

时间:2014-05-08 10:11:18

标签: android layout android-studio

我收到此错误:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

我认为错误是由于我在线性布局中有相对布局 我找了同样的问题:Set RelativeLayout layout params programmatically - throws ClassCastException

但这没有帮助

我的代码:

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>

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

嗯,看起来mT1layoutScore的参数是LinearLayout.LayoutParams,但是mT2layout的参数是RelativeLayout.LayoutParams

所以,我认为你应该创建

RelativeLayout.LayoutParams paramsT2score = (RelativeLayout.LayoutParams)mT2layout.getLayoutParams();

然后将所有需要的参数从paramsT1score复制到paramsT2score,然后添加

mT2layout.setLayoutParams(paramsT2score);
相关问题