Imageview and textview not displaying in view?

2019-02-26 12:17发布

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:

adding of Linear Layout:

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

Linear Layout:

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

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.

1条回答
欢心
2楼-- · 2019-02-26 13:05

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

before the layout on this line:

getChildAt(i).layout(xy.x, xy.y, xy.x + childSize, xy.y + childSize);
查看更多
登录 后发表回答