Java 如何将动态权重设置为LinearLayout?
在我的应用程序中,我希望将动态权重设置为Java 如何将动态权重设置为LinearLayout?,java,android,android-layout,Java,Android,Android Layout,在我的应用程序中,我希望将动态权重设置为LinearLayout,为此,我编写了以下代码,但在运行应用程序时显示强制关闭错误 在logCat中强制关闭消息: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams <?xml version="1.0" encoding="utf-8
LinearLayout
,为此,我编写了以下代码,但在运行应用程序时显示强制关闭错误
在logCat中强制关闭消息:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
<?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>
我的XML代码:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
<?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>
如何解决此问题?如果要动态设置权重和,请使用以下代码:
dialogEndCount_buttonsLay.setWeightSum(3f);
我希望能帮助你你把线性布局和相对布局混为一谈。将布局更改为使用LinearLayout,则权重是LayoutParams上的可用属性
val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
params.weight = 1f
您没有将java线性布局链接到XML布局id。添加此
dialogEndCount\u buttonsLay=(LinearLayout)findViewById(R.id.dialogEndCount\u buttonsLay)代码>@Marech,对于初始化布局,我使用ButterKnife!