【问题标题】:Inflated layout's children not showing膨胀的布局的孩子没有显示
【发布时间】:2015-11-04 21:03:51
【问题描述】:

我正在使用DraggableGridView from here

我之前所做的是将一个简单的以编程方式构建的ImageViews 添加到网格中。效果很好。

我现在尝试添加Layout。我在RelativeLayout 中尝试了RelativeLayout、Framelayout、FrameLayout

这是我现在的代码:

/**
 * Rebuild the grid view, e.g. after the adapter has been filled or a backup is restored
 */
private void renewGrid()
{
    dgv.removeAllViews();
    for(int i = 0; i < adapter.getCount(); i++)
    {
        LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        dgv = (DraggableGridView) inflater.inflate(R.layout.grid_item_layout, dgv);
        FrameLayout fl = (FrameLayout) dgv.findViewById(R.id.grid_images);
        ImageView icon = (ImageView) fl.findViewById(R.id.qstile);
        icon.setImageDrawable(res.getDrawable(res.getIdentifier("qstile_" + adapter.getItem(i), "drawable", packagename)));
    }
}

按照grid_item_layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <FrameLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/grid_images">
        <ImageView
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/icon_delete"/>

        <ImageView
            android:id="@+id/qstile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </FrameLayout>

    <TextView
        android:id="@+id/invisible_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text=""
        android:visibility="gone" />

</RelativeLayout>

RelativeLayout 很好地膨胀到网格中。我可以通过 DDMS 和“转储视图层次结构”功能观察到这一点。该树显示了网格内的所有 RelativeLayout,但每个都没有任何子级。

但是……那不是真的。如果我单步执行代码并观察膨胀的布局,我可以看到孩子们。所以他们已经设置好了,就像在 XML 中所说的那样被添加,但不要被绘制。

网格子节点上的OnItemClick listener 也有效...

关于我在这里缺少什么的任何提示?我已经尝试了几种方法,甚至以编程方式创建完整的布局,然后将其作为子项添加到网格中。仍然没有运气。没有一个孩子被添加。

这可能是使用过的DraggableGridView 的问题吗?

【问题讨论】:

    标签: java android android-layout android-xml


    【解决方案1】:

    又搜索了几个小时后,我在 this SO thread 中找到了修复方法

    基本上我现在从FrameLayout 扩展DraggableGridView 而不是ViewGroup。这对我没有任何明显的副作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多