addView 添加视图但没有显示它

Ric*_*reu 3 java android android-studio

我知道已经有人问过这个问题,但我尝试了一切,但无法解决我的问题。

当我以编程方式创建视图时,它们肯定会被添加。我检查了调试器,一切都在它的位置,甚至父视图的高度也变得更大,因为它们正在使用空间。但我看不到他们。就好像它们位于其他视图下方或不可见(但它们不是。我检查了很多次......)。

在此输入图像描述

这是我尝试插入视图的 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);
        llhItem.setLayoutParams(llhItemParams);
        llhItem.setOrientation(LinearLayout.HORIZONTAL);
        //endregion

        LinearLayout.LayoutParams tvInformationsParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //region Create tvInformationTitle
        TextView tvInformationTitle = new TextView(context);
        tvInformationTitle.setLayoutParams(tvInformationsParams);
        tvInformationTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        if (Build.VERSION.SDK_INT < 23){
            tvInformationTitle.setTextAppearance(context, R.style.OpenSansRegular);
        } else {
            tvInformationTitle.setTextAppearance(R.style.OpenSansRegular);
        }
        tvInformationTitle.setText(cardViewItem.getInformationList().get(i)[0]);
        //endregion

        //region Create tvInformation
        TextView tvInformation = new TextView(context);
        tvInformation.setLayoutParams(tvInformationsParams);
        tvInformation.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        if (Build.VERSION.SDK_INT < 23){
            tvInformation.setTextAppearance(context, R.style.OpenSansLight);
        } else {
            tvInformation.setTextAppearance(R.style.OpenSansLight);
        }
        tvInformation.setText(cardViewItem.getInformationList().get(i)[1]);
        //endregion

        llhItem.addView(tvInformationTitle);
        llhItem.addView(tvInformation);

        holder.llvInformation.addView(llhItem);
    }
Run Code Online (Sandbox Code Playgroud)

基本上我想要实现的是拥有一个回收器视图,并且每个项目只有一个标题、一个溢出按钮,但可以有多个信息行。这是我之前在 xml 中硬编码为原型的打印结果。

在此输入图像描述

我知道一些可能有效的替代方法,但现在我希望这样,因为一切都按应有的方式工作,视图只是“不可见”。

Chi*_*ndo 5

不得不使用layout.post

holder.llvInformation.post( new Runnable() {
    @Override
    public void run() {
        holder.llvInformation.addView(llhItem);
    }
});
Run Code Online (Sandbox Code Playgroud)