gpt4 book ai didi

java - 如何以编程方式设计线性布局

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

我怎样才能以编程方式做这样的事情? MaterialCardView 中必须有一个复选框和两个垂直 TextView 。我尝试使用 Linearlayout 但无法解决它。 enter image description here

 LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT

);
linearLayout.setOrientation(LinearLayout.VERTICAL);
mCardView=new MaterialCardView(this);
mCardView.setLayoutParams(layoutparams);
mCardView.setElevation(2);
linearLayout.addView(mCardView);

LinearLayout linearLayout1=new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
linearLayout1.setLayoutParams(layoutparams);
linearLayout.addView(linearLayout1)

checkBox=new CheckBox(this);
linearLayout1.addView(checkBox);
txt_baslik=new TextView(this);
txt_tarih=new TextView(this);
txt_baslik.setLayoutParams(layoutparams);
txt_tarih.setLayoutParams(layoutparams);
txt_baslik.setText("deneme baslik");
txt_tarih.setText("deneme tarih");
linearLayout1.addView(txt_baslik);
linearLayout1.addView(txt_baslik);

LinearLayout linearLayout2=new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.VERTICAL);
linearLayout2.setLayoutParams(layoutparams);
linearLayout.addView(linearLayout2);

我很困惑

最佳答案

你可以这样做:

enter image description here

LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout linearLayout=new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);

CardView mCardView=new MaterialCardView(this);
mCardView.setLayoutParams(layoutparams);
mCardView.setElevation(2);

CheckBox checkBox=new CheckBox(this);
LinearLayout.LayoutParams checkBoxParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
checkBoxParam.gravity = Gravity.CENTER;
checkBox.setLayoutParams(checkBoxParam);

LinearLayout verticalLinLayout=new LinearLayout(this);
verticalLinLayout.setOrientation(LinearLayout.VERTICAL);
verticalLinLayout.setLayoutParams(layoutparams);

TextView txt_baslik=new TextView(this);
TextView txt_tarih=new TextView(this);

txt_baslik.setLayoutParams(layoutparams);
txt_tarih.setLayoutParams(layoutparams);

txt_baslik.setText("deneme baslik");
txt_tarih.setText("deneme tarih");

verticalLinLayout.removeAllViews();
verticalLinLayout.addView(txt_baslik);
verticalLinLayout.addView(txt_tarih);
linearLayout.addView(checkBox);
linearLayout.addView(verticalLinLayout);
mCardView.addView(linearLayout);

最后不要忘记将 mCardView 添加到您的父 View 中。

关于java - 如何以编程方式设计线性布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61299389/

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