有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 Java。lang.classcastexception linearlayout。layoutParams不能强制转换为framelayout。布局参数

这是XML代码

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.v4.widget.DrawerLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/drawer_layout"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent" >

    <安卓.support.v4.widget.SwipeRefreshLayout
        安卓:id="@+id/swipe"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:background="@color/light_grey" >

        <ScrollView
            安卓:id="@+id/scrollView1"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:scrollbars="none" >

            <RelativeLayout
                安卓:layout_width="match_parent"
                安卓:layout_height="match_parent"
                安卓:paddingBottom="@dimen/activity_vertical_margin"
                安卓:paddingLeft="@dimen/activity_horizontal_margin"
                安卓:paddingRight="@dimen/activity_horizontal_margin"
                安卓:paddingTop="@dimen/activity_vertical_margin" >

                <RelativeLayout
                    安卓:id="@+id/logolayout"
                    安卓:layout_width="match_parent"
                    安卓:layout_height="wrap_content"
                    安卓:background="@drawable/layout_bg" >

                    <ImageView
                        安卓:id="@+id/company_logo"
                        安卓:layout_width="80dp"
                        安卓:layout_height="80dp"
                        安卓:layout_alignParentTop="true"
                        安卓:layout_marginLeft="5dp"
                        安卓:layout_marginTop="5dp"
                        安卓:background="@drawable/iv_bg"
                        安卓:scaleType="centerInside" />

                    <TextView
                        安卓:id="@+id/company_name"
                        安卓:layout_width="fill_parent"
                        安卓:layout_height="wrap_content"
                        安卓:layout_marginLeft="10dp"
                        安卓:layout_marginTop="8dp"
                        安卓:layout_toRightOf="@+id/company_logo"
                        安卓:gravity="center"
                        安卓:text="The D Corporation Inc."
                        安卓:textSize="18dp"
                        安卓:textStyle="bold" />

                    <TextView
                        安卓:id="@+id/sss_name"
                        安卓:layout_width="fill_parent"
                        安卓:layout_height="wrap_content"
                        安卓:layout_below="@+id/company_name"
                        安卓:layout_marginLeft="10dp"
                        安卓:layout_marginTop="1dp"
                        安卓:layout_toRightOf="@+id/company_logo"
                        安卓:gravity="center"
                        安卓:text="RD Sales Management"
                        安卓:textSize="16dp"
                        安卓:textStyle="bold" />

                    <TextView
                        安卓:id="@+id/login_user_name"
                        安卓:layout_width="fill_parent"
                        安卓:layout_height="wrap_content"
                        安卓:layout_below="@+id/sss_name"
                        安卓:layout_marginLeft="10dp"
                        安卓:layout_marginTop="1dp"
                        安卓:layout_toRightOf="@+id/company_logo"
                        安卓:gravity="center"
                        安卓:singleLine="true"
                        安卓:textColor="#E55451"
                        安卓:textSize="15dp"
                        安卓:textStyle="bold" />
                </RelativeLayout>

                <RelativeLayout
                    安卓:id="@+id/workorderlayout"
                    安卓:layout_width="match_parent"
                    安卓:layout_height="wrap_content"
                    安卓:layout_below="@+id/logolayout"
                    安卓:layout_marginTop="8dp"
                    安卓:background="@drawable/layout_bg" >

                    <TextView
                        安卓:id="@+id/header_text"
                        安卓:layout_width="wrap_content"
                        安卓:layout_height="wrap_content"
                        安卓:layout_marginLeft="10dp"
                        安卓:layout_marginTop="12dp"
                        安卓:gravity="left"
                        安卓:text="Today’s Activity"
                        安卓:textSize="17dp"
                        安卓:textStyle="bold" />

                    <Button
                        安卓:id="@+id/id_linesp"
                        安卓:layout_width="match_parent"
                        安卓:layout_height="1dp"
                        安卓:layout_below="@+id/header_text"
                        安卓:layout_marginTop="8dp"
                        安卓:background="@color/light_grey" />

                    <HorizontalScrollView
                        安卓:layout_below="@+id/id_linesp"
                        安卓:layout_width="match_parent"
                        安卓:layout_height="wrap_content" >

                        <LinearLayout
                            安卓:id="@+id/lylat"
                            安卓:layout_width="fill_parent"
                            安卓:layout_height="wrap_content"
                            安卓:layout_marginTop="10dp"
                            安卓:orientation="horizontal"
                            安卓:paddingLeft="2dp"
                            安卓:paddingRight="2dp"
                            安卓:weightSum="3" >

                            <TableLayout
                                安卓:id="@+id/invoice_st"
                                安卓:layout_width="0dp"
                                安卓:layout_height="88dp"
                                安卓:layout_marginRight="5dp"
                                安卓:layout_weight="1"
                                安卓:background="@drawable/btn_bg"
                                安卓:paddingBottom="6dip" >

                                <TableRow>

。。。。。。。静态代码流

我在课堂上使用了下面的一段代码来设置Horizontall scrollview中线性布局的宽度

mLinearLayout = (LinearLayout) findViewById(R.id.lylat);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);


        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels/4, HorizontalScrollView.LayoutParams.MATCH_PARENT);
        mLinearLayout.setLayoutParams(params);

在此处设置宽度时应用程序崩溃HorizontalScrollView.LayoutParams.MATCH_PARENT

上面写着java。lang.classcastexception linearLayout。layoutparams不能强制转换为framelayout。layoutparams,,非常感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

    可以为视图指定的布局参数类型不取决于视图的类,而是取决于其容器的类。在本例中,容器是一个HorizontalScrollView,它需要子容器是一个FrameLayout.LayoutParameters。如果只需设置宽度和高度,请执行以下操作:

    ViewGroup.LayoutParams params = mLinearLayout.getParams();
    params.layout_width = metrics.widthPixels/4;
    params.layout_height = ViewGroup.LayoutParams.MATCH_PARENT;
    mLinearLayout.setLayoutParams(params);
    

    换句话说,使用已由其父级分配给mLinearLayoutLayoutParams