android - 使用adapterview时无法在 View 组中绘制 subview

标签 android android-layout android-adapterview

我试图让适配器 View 正确绘制其 subview ,但无法绘制适配器返回的 View 的 subview 。具体来说,我的适配器应该吐出像这样的简单 View :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">

        <TextView
            android:id="@+id/photo_cell_contents"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10px" 
            android:text="@string/photo_cell_default_text" 
            android:textColor="#000000" 
            android:textSize="80px"/>

</LinearLayout>

适配器的 getView() 方法确实被调用并且确实将此 View 的实例返回给适配器 View 。我还在我的适配器 View 中放置了断点,并且可以看到那里有一个正确的 View 层次结构 - 带有子 TextView 对象的根线性布局。但是,当适配器 View 布局并绘制适配器返回的 subview 时,不会绘制内部 TextView 。我看到了对应于每个单元格的实际彩色框,但没有看到应该出现在里面的文本。有谁知道为什么会发生这种情况?下面是布置 subview 的适配器 View 的 onLayout() 方法中的代码(当我 setAdapter() 时添加并在我滚动适配器 View 时更新):

    for (int idx = 0; idx < this.getChildCount(); idx++) {
        if (idx != dragged)
        {
            Point xy = getCoorFromIndex(idx);
            View child = getChildAt(idx);
            child.layout(xy.x, xy.y, xy.x + childSize, xy.y + childSize); //view group will layout the children based on the sizes determined for available columns
            int left = child.getLeft();
            int right = child.getRight();
            int top = child.getTop();
            View tv = ((ViewGroup)child).getChildAt(0);
            int tvleft = tv.getLeft();
            int tvright = tv.getRight();
            int tvtop = tv.getTop();

            Log.i("dgv", "Here is info about main view ... left =  " + left + "; right = " + right + "; top = " + top);
            Log.i("dgv", "Here is info about text view ... left =  " + tvleft + "; right = " + tvright + "; top = " + tvtop);


        }
    }

最佳答案

问题是我需要在适配器 View 的布局方法中测量和布局 subview 。添加代码以测量 subview 然后调用 child.layout() 后, View 正确显示。希望这可以帮助。

关于android - 使用adapterview时无法在 View 组中绘制 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521034/

相关文章:

java - (EditText)Android 联系人列表获取电话号码

android - Android中的自定义adapterview setselection实现

android - 自定义AdapterView

android - 单击 TextView 时显示 ArrayAdapter

android - 卡接口(interface) : Can't fit contents and text is clipped

android - 从 Android 触摸事件中松开 X 和 Y 值,以检测 View 外的移动

android - 获取钛合金日期选择器

缩放时Android MapView出现内存不足错误

javascript - 如何在适用于 Android 平台的 PhoneGap 应用程序中接收即时通知,告知您收件箱中有来自管理员的新消息

android - 解决意外顶级异常的最佳方法是什么?