首页 > 解决方案 > 如何为 LinearLayout 动态设置权重?

问题描述

在我的应用程序中,我想动态设置权重LinearLayout为此我写了下面的代码,但是在运行应用程序时显示强制关闭错误!

在 logCat 中强制关闭消息:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@color/white"
    tools:context=".activity.UpdateActivity">
    <!--Header-->
    <RelativeLayout
        android:id="@+id/dialogEndCount_headerLay"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size120"
        android:background="@color/tabHomeColor">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="@dimen/size40"
            android:text="@string/updateDialogTitle"
            android:textColor="@color/white"
            android:textSize="@dimen/font16" />

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/dialogUpdate_animate"
            android:layout_width="@dimen/size150"
            android:layout_height="@dimen/size150"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_marginLeft="@dimen/size20"
            app:lottie_autoPlay="true"
            app:lottie_colorFilter="@color/white"
            app:lottie_fileName="update.json"
            app:lottie_loop="true" />

    </RelativeLayout>
    <!--Content-->
    <RelativeLayout
        android:id="@+id/dialogEndCount_contentLay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/dialogEndCount_buttonsLay"
        android:layout_below="@+id/dialogEndCount_headerLay"
        android:layout_marginBottom="@dimen/size10"
        android:layout_marginTop="@dimen/size10"
        android:background="@drawable/bg_dialog_vip_white">
        <!--Type-->
        <TextView
            android:id="@+id/dialogUpdate_infoTypeTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="@dimen/size15"
            android:layout_marginRight="@dimen/size15"
            android:layout_marginTop="@dimen/size10"
            android:gravity="center_vertical"
            android:text="نوع بروز رسانی : اجباری"
            android:textColor="@color/black"
            android:textSize="@dimen/font14" />
        <!--Content-->
        <ScrollView
            android:id="@+id/dialogUpdate_infoContentScrollLay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/dialogUpdate_downloadProgress"
            android:layout_alignRight="@+id/dialogUpdate_infoTypeTxt"
            android:layout_below="@+id/dialogUpdate_infoTypeTxt"
            android:layout_marginLeft="@dimen/size15"
            android:layout_marginTop="@dimen/size20">

            <RelativeLayout
                android:id="@+id/dialogUpdate_infoContentLay"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/dialogUpdate_infoContentTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:gravity="center_vertical"
                    android:text="@string/updateContent"
                    android:textColor="@color/black"
                    android:textSize="@dimen/font14" />

                <TextView
                    android:id="@+id/dialogUpdate_infoContentTxt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/dialogUpdate_infoContentTitle"
                    android:layout_marginTop="@dimen/size5"
                    android:gravity="center_vertical"
                    android:textColor="@color/favColorON"
                    android:textSize="@dimen/font14" />

            </RelativeLayout>

        </ScrollView>
        <!--Download layout-->
        <com.tellfa.colony.view.DownloadProgressView
            android:id="@+id/dialogUpdate_downloadProgress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/dialogUpdate_infoTypeTxt"
            android:layout_marginLeft="@dimen/size15"
            android:layout_marginTop="@dimen/size10" />

    </RelativeLayout>
    <!--Buttons-->
    <LinearLayout
        android:id="@+id/dialogEndCount_buttonsLay"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size60"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:weightSum="3">
        <!--Cancel-->
        <android.support.v7.widget.CardView
            android:id="@+id/dialogUpdate_cancelBtn"
            android:layout_width="@dimen/margin_0dp"
            android:layout_height="@dimen/size50"
            android:layout_marginBottom="@dimen/size5"
            android:layout_marginLeft="@dimen/size5"
            android:layout_marginRight="@dimen/size5"
            android:layout_marginTop="@dimen/size5"
            android:layout_weight="1"
            app:cardBackgroundColor="@color/red"
            app:cardCornerRadius="@dimen/size3"
            app:cardElevation="@dimen/size3">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/dialogUpdate_cancelBtnImg"
                    android:layout_width="@dimen/size25"
                    android:layout_height="@dimen/size25"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="@dimen/size5"
                    android:src="@drawable/ic_close"
                    android:tint="@color/white" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_toRightOf="@+id/dialogUpdate_cancelBtnImg"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="@string/updateCancel"
                    android:textColor="@color/white"
                    android:textSize="@dimen/font14" />

            </RelativeLayout>

        </android.support.v7.widget.CardView>
        <!--Direct-->
        <android.support.v7.widget.CardView
            android:id="@+id/dialogUpdate_directBtn"
            android:layout_width="@dimen/margin_0dp"
            android:layout_height="@dimen/size50"
            android:layout_marginBottom="@dimen/size5"
            android:layout_marginLeft="@dimen/size5"
            android:layout_marginRight="@dimen/size5"
            android:layout_marginTop="@dimen/size5"
            android:layout_weight="1"
            app:cardBackgroundColor="@color/green"
            app:cardCornerRadius="@dimen/size3"
            app:cardElevation="@dimen/size2">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/dialogUpdate_directBtnImg"
                    android:layout_width="@dimen/size25"
                    android:layout_height="@dimen/size25"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="@dimen/size5"
                    android:src="@drawable/appro_ic_download_circle"
                    android:tint="@color/white" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_toRightOf="@+id/dialogUpdate_directBtnImg"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:layout_marginRight="@dimen/size1"
                    android:singleLine="true"
                    android:text="@string/updateOK"
                    android:textColor="@color/white"
                    android:textSize="@dimen/font12" />

            </RelativeLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

</RelativeLayout>

我的 JAVA 代码:

public class UpdateActivity extends BaseActivity {

    @BindView(R.id.dialogEndCount_buttonsLay)
    LinearLayout dialogEndCount_buttonsLay;
    private Window window;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        //Initialize
        ButterKnife.bind(this);
        activity = this;
        window = activity.getWindow();
        //Set color to statusBar
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setStatusBarColor(getResources().getColor(R.color.tabHomeColor));
        }
        //Set dynamically weight
        dialogEndCount_buttonsLay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

    }
}

我该如何解决这个问题?

标签: javaandroidandroid-layout

解决方案


如果您想动态设置weightSum,只需使用以下代码:

dialogEndCount_buttonsLay.setWeightSum(3f);

我希望能帮助你


推荐阅读