Imageview and textview not displaying in view?

99封情书 提交于 2019-12-04 04:55:12

问题


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:


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


来源:https://stackoverflow.com/questions/13699426/imageview-and-textview-not-displaying-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!