【问题标题】:Views are invisible after they was added into ViewGroup视图添加到 ViewGroup 后不可见
【发布时间】:2016-07-28 09:11:55
【问题描述】:

我正在以编程方式将视图添加到FrameLayout

public class PixelGridView extends FrameLayout {
...
     public void addViewWithoutCoords(TableView view, int column, int row) {
            float x = column * mCellSideSize;
            float y = row * mCellSideSize;

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(lp);

            view.setLeft(0);
            view.setTop(0);

            view.setX(x);
            view.setY(y);
            view.setVisibility(VISIBLE);
            addView(view);
        }
...
}

然而,它们都是不可见的。 getChildCount() 返回所有这些计数。 getVisibility() 为每个添加的视图也返回 VISIBLE

我可以将此类视图从另一个 ViewGroup 拖放到我的 Framelayout 中,当我这样做时,所有之前添加的视图都会变得可见。

view布局文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:id="@+id/ivTable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
    <EditText
        android:id="@+id/ivTableName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/style_table_grey"
        android:textColor="@android:color/white"/>
</RelativeLayout>

即使我在不​​拖动的情况下添加新视图,一切都会变得可见。

【问题讨论】:

    标签: java android view viewgroup


    【解决方案1】:

    FrameLayoutRelativeLayout 的项目视图可以重叠。如果两个 item 视图在 FrameLayout 的相同位置,则第一个加载的 item 将被后面的 item 覆盖。

    【讨论】:

    • 你的意思是RelativeLayout的浏览量可以在FrameLayout下面吗?
    • 你成就了我的一天))我将代码放入“ViewTreeObserver.OnGlobalLayoutListener()”,就是这样)
    • 我的意思是RelativeLayoutFrameLayout中的所有item view如果在相同的位置,它们会相互重叠或覆盖。
    • @Lay'O 我很高兴听到这个消息:)
    【解决方案2】:

    尝试在你的安卓设备的开发者选项中打开显示布局边界,并找到你的项目的位置。

    【讨论】:

    • 它表明我的容器内没有视图(但 getChildCount() 返回它们)。我试图调用 invalidate() 但它没有帮助
    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多