ImageView的和TextView中没有显示的看法?看法、ImageView、TextView

2023-09-07 15:31:42 作者:我长的丑我活的久阿i

我试图夸大布局,并把它添加到拖动网格视图,但我得到的是一个黄色的sqaure。可拖动视图的addView()方法,只捡了一个单一视图。例如,如果我添加一个TextView或ImageView的那么TextView的将被显示。如果我添加的LinearLayout线性布局则只化背景(又名的LinearLayout)将被显示出来。但不是linearlayouts孩子的(图像和文本视图)。该DGV(可拖动的GridView)延伸的V​​iewGroup这里的code:

I'm trying to inflate a layout and add it to the draggable grid view But all I get is a yellow sqaure. The draggable view's addView() method is only picking up a single view. For example, if I add a textView or imageView then the textview will be displayed. If I add a linear layout then only the backgroud (aka the linearlayout) of the linearlayout will be displayed. But not the linearlayouts childs (image and text view). The dgv (draggable Gridview) extends ViewGroup Here's the code:

增加线性布局:

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,并做我想做的。但是,这使哈克并没有可扩展性。任何帮助将是AP preciated。

Any ideas? Do I need to provide more info? My only solution is to customize imageview and override ondraw and do what I want. But that's so hacky and not scalable. Any help would be appreciated.

推荐答案

这是一个已知的DraggableGridView问题 - 遗憾的是 - 我还没有得到解决,以固定。当我写DGV,我并没有完全掌握如何看法布局。您可以尝试有铺设之前DGV测量每个孩子。添加类似:

This is a known problem with the DraggableGridView that -- unfortunately -- I haven't gotten around to fixing. When I wrote DGV, I didn't entirely grasp how views were laid out. You might try having DGV measure each child before laying it out. Adding something like:

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);