java.lang.classcastException Linearlayout.layoutParams 无法转换为framelayout.layoutparams

标签 java android android-layout layout

这是 XML 代码,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                <TableRow>

......仍然是代码流

我在类中使用了以下代码来设置水平 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);

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

它说 java.lang.classcastexception LinearLayout.layoutparams 无法转换为framelayout.layoutparams ,,您的帮助将非常感激

最佳答案

View 可以分配的布局参数的类型不取决于 View 的类,而是取决于其容器的类。在本例中,容器是一个 Horizo​​ntalScrollView,它需要子级的 FrameLayout.LayoutParameters。如果您只想设置宽度和高度,请执行以下操作:

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

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

关于java.lang.classcastException Linearlayout.layoutParams 无法转换为framelayout.layoutparams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25904981/

相关文章:

javascript - Android - 从 Firebase 数据库中删除节点的问题

android - 这样的渐变?

android - TextView lineSpacingMultiplier 小于零会截断最后一行

android - 如何更改 ImageView 中图像的颜色?

javascript - 在 Javascript 中将 Java 颜色转换为 RGB

java - 无法使用 Intellij IDEA 运行 Java 代码

Java连接访问数据库失败?

android - 从 ViewPager 返回 Activity 结果

java - 设备旋转时丢失 'MediaPlayer'(及其他变量)

java - Hibernate @ManyToMany 不使用 joinTable 并生成笛卡尔而不是内连接