猿问

Android:LinearLayout.addView 不渲染添加的视图

我试图以LinearLayout编程方式(在循环中)将两个文本视图添加到 a 中,然后依次将其添加到LinearLayout布局文件中定义的 a 中。代码运行时没有错误,并且在评估 getChildCount 时我得到了预期值,但设备上绝对没有渲染任何内容。


XML:


<LinearLayout

        android:id="@+id/llNotes"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginStart="1dp"

        android:layout_marginLeft="1dp"

        android:layout_marginEnd="1dp"

        android:layout_marginRight="1dp"

        android:layout_marginBottom="@dimen/activity_vertical_margin_quarter"

        android:background="@drawable/rounded_bottom_corners"

        android:orientation="vertical"

        android:paddingTop="@dimen/activity_vertical_margin_half"

        android:paddingBottom="@dimen/activity_vertical_margin" />

爪哇:


for (Note MyNote : foo.GetNotes()) {

            LinearLayout llNoteParent = new LinearLayout(this);

            TextView tvNoteHeader = new TextView(this);

            TextView tvNoteValue = new TextView(this);


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

            llParams.gravity = Gravity.CENTER_HORIZONTAL;

            llNoteParent.setLayoutParams(llParams);

            llNoteParent.setOrientation(LinearLayout.HORIZONTAL);

            llNoteParent.setBackgroundColor(ContextCompat.getColor(this, R.color.white));

            llNoteParent.setPadding(R.dimen.activity_horizontal_margin, R.dimen.activity_vertical_margin_half, R.dimen.activity_horizontal_margin, R.dimen.activity_horizontal_margin);

        }


鸿蒙传说
浏览 195回答 2
2回答

一只萌萌小番薯

您无法将带有 dp 的数字传递到 setPadding 方法中setPadding(R.dimen.activity_horizontal_margin, R.dimen.activity_vertical_margin_half, R.dimen.activity_horizontal_margin, R.dimen.activity_horizontal_margin);您需要将整数传递给 setPadding 方法。1-尝试这样的事情llNoteParent.setPadding(20,20,20,20);tvNoteHeader.setPadding(20,20,20,20);2-或用户integer.xml&nbsp; &nbsp; &nbsp;for (Note MyNote : foo.GetNotes()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LinearLayout llNoteParent = new LinearLayout(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvNoteHeader = new TextView(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvNoteValue = new TextView(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llParams.gravity = Gravity.CENTER_HORIZONTAL;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llNoteParent.setLayoutParams(llParams);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llNoteParent.setOrientation(LinearLayout.HORIZONTAL);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llNoteParent.setBackgroundColor(ContextCompat.getColor(this, R.color.white));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llNoteParent.setPadding(R.integer.activity_horizontal_margin, R.integer.activity_vertical_margin_half, R.integer.activity_horizontal_margin, R.integer.activity_horizontal_margin);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LinearLayout.LayoutParams tvNoteHeaderParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteHeaderParams.weight = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteHeader.setLayoutParams(tvNoteHeaderParams);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteHeader.setPadding(R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteHeader.setBackground(getDrawable(R.drawable.left_text_field));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteHeader.setGravity(Gravity.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteHeader.setText(MyNote.GetAbbreviatedText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LinearLayout.LayoutParams tvNoteValueParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteValueParams.weight = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteValue.setLayoutParams(tvNoteValueParams);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteValue.setPadding(R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteValue.setBackground(getDrawable(R.drawable.right_text_field));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvNoteValue.setText(MyNote.GetText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llNoteParent.addView(tvNoteHeader);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llNoteParent.addView(tvNoteValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; llNotes.addView(llNoteParent);&nbsp;}注意:将值添加到integer.xml文件的values 中。

慕容3067478

你必须使用,float&nbsp;horizontalMargin&nbsp;=&nbsp;getResources() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getDimension(R.dimen.R.dimen.activity_horizontal_margin); float&nbsp;verticleMargin&nbsp;=&nbsp;getResources() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getDimension(R.dimen.R.dimen.activity_vertical_margin_half);设置填充后,llNoteParent.setPadding(horizontalMargin,verticleMargin,&nbsp;horizontalMargin,&nbsp;horizontalMargin); tvNoteHeader.setPadding(verticleMargin,&nbsp;verticleMargin,&nbsp;verticleMargin,&nbsp;verticleMargin); tvNoteValue.setPadding(verticleMargin,&nbsp;verticleMargin,&nbsp;verticleMargin,&nbsp;verticleMargin);
随时随地看视频慕课网APP

相关分类

Java
我要回答