android - Imageview 和 textview 未显示在 View 中?

标签 android android-imageview textview android-gridview

我正在尝试膨胀布局并将其添加到可拖动 GridView 但我得到的只是一个黄色方 block 。可拖动 View 的 addView() 方法仅拾取单个 View 。例如,如果我添加一个textView或imageView,那么将显示textview。如果我添加线性布局,则只会显示线性布局的背景(也称为线性布局)。但不是线性布局 child (图像和 TextView )。 dgv(可拖动的 Gridview)扩展了 ViewGroup 代码如下:

添加线性布局:

LayoutInflater inflater = LayoutInflater.from (getActivity ());
View cellView = inflater.inflate (R.layout.celllayout, null);
if (cellView != null)
Log.d ("Snap", "cellView != null");
TextView textView = (TextView) cellView.findViewById (R.id.grid_label);
textView.setText (word);
ImageView imageView =  (ImageView) cellView.findViewById (R.id.grid_image);
imageView.setImageBitmap (getThumb (word));

dgv.addView (cellView);

线性布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF000"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >

<TextView
android:id="@+id/grid_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"
android:text="@string/grid_label"
android:textColor="#c31d36"
android:textSize="15sp" />
<ImageView
android:id="@+id/grid_image"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_gravity="center_horizontal"
android:contentDescription="An Image" 
android:background="#ffff0000"/>

</LinearLayout>

dgv:

@Override
public void addView (View child) {
    super.addView (child);
    Log.d ("Draggable", "Draggable-addView");

    newPositions.add (-1);
}

有什么想法吗?我需要提供更多信息吗?我唯一的解决方案是自定义 imageview 并覆盖 ondraw 并做我想做的事情。但这太老套了而且不可扩展。任何帮助将不胜感激。

最佳答案

这是 DraggableGridView 的一个已知问题,不幸的是,我还没有抽出时间来修复。当我写 DGV 时,我并没有完全掌握 View 是如何布局的。您可以尝试让 DGV 在布置每个 child 之前对其进行测量。添加类似的内容:

getChildAt(i).measure(MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY));

在该行布局之前:

getChildAt(i).layout(xy.x, xy.y, xy.x + childSize, xy.y + childSize);

关于android - Imageview 和 textview 未显示在 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13699426/

相关文章:

条目 : packageOutputFile=null 中的 android studio 空值

java - 在 Android 中使用 Google 帐户登录

android - TextView实体阴影

android - 宽度大于屏幕宽度的ImageView

android - 如何在 LinearLayout 单元格中对齐两个 TextView?

Java:获取键集值

android - ViewPager 内的 ViewPager

Android,如何从 UI 中删除 View ?

java - 在 ImageView 上设置layout_marginTop?

android - 使用 Picasso 加载大图像