gpt4 book ai didi

java - 如何动态设置LinearLayout的权重?

转载 作者:行者123 更新时间:2023-12-02 11:06:59 25 4
gpt4 key购买 nike

在我的应用程序中,我想动态地将权重设置为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));

}
}

如何解决这个问题?

最佳答案

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

dialogEndCount_buttonsLay.setWeightSum(3f);

希望对你有帮助

关于java - 如何动态设置LinearLayout的权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com