我知道已经有人问过这个问题,但我尝试了一切,但无法解决我的问题。
当我以编程方式创建视图时,它们肯定会被添加。我检查了调试器,一切都在它的位置,甚至父视图的高度也变得更大,因为它们正在使用空间。但我看不到他们。就好像它们位于其他视图下方或不可见(但它们不是。我检查了很多次......)。
这是我尝试插入视图的 xml 代码。我想将它们插入光标所在的位置(标记信息的位置)。我只是向您展示它最终的样子,但这部分将以编程方式添加。
<LinearLayout
android:id="@+id/llhTestItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tvInformationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
fontPath="fonts/OpenSans-Regular.ttf"
android:text="Sub title: "/> <!-- tvInformationTitle -->
<TextView
android:id="@+id/tvInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
fontPath="fonts/OpenSans-Light.ttf"
android:text="information"/> <!-- tvInformation -->
</LinearLayout> <!-- information -->
Run Code Online (Sandbox Code Playgroud)
您可以在下面看到我用来添加视图的代码,就像上面的 xml 中一样。
@Override
public void onBindViewHolder(SetupViewerHolder holder, int position) {
CardViewItem cardViewItem = cardViewItemList.get(position);
holder.tvTitle.setText(cardViewItem.getCardTitle());
for (int i = 0; i < cardViewItem.getInformationList().size(); i++){
//region Create llhItem
LinearLayout.LayoutParams llhItemParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llhItemParams.topMargin = dipToPixels(6);
LinearLayout llhItem = new LinearLayout(context); …Run Code Online (Sandbox Code Playgroud)