android-Imageview和textview不在视图中显示?
作者:互联网
我正在尝试增加一个布局并将其添加到可拖动的网格视图中,但是我得到的只是一个黄色正方形.可拖动视图的addView()方法仅拾取单个视图.例如,如果我添加textView或imageView,则将显示textview.如果添加线性布局,则只会显示linearlayout的背景(又称linearlayout).但不是linearlayouts子级(图像和文本视图). 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时,我并不完全理解视图的布局方式.您可以尝试让DGV在布置每个孩子之前对其进行测量.添加类似:
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-gridview,android,textview,android-imageview 来源: https://codeday.me/bug/20191011/1890538.html