Android 图像不显示在gridView中

Android 图像不显示在gridView中,android,gridview,android-gridview,Android,Gridview,Android Gridview,我这里有一个gridView: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_drag_and_drop_app" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_paren

我这里有一个gridView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_drag_and_drop_app"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SlidingDrawer
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:orientation="horizontal"
    android:layout_marginTop="200dp"
    android:content="@+id/content"
    android:handle="@+id/handle" >

    <Button
        android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/arrow" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical"
        android:background="#00FF00">

            <!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/Search_applications"
    android:inputType="textVisiblePassword"/>

<ListView
    android:id="@+id/lvApps"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"       
    />
    </LinearLayout>
    </SlidingDrawer>

        <Button
        android:id="@+id/btnLinkToPersonalize"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:background="@null"
        android:text="@string/Personalize"
        android:textColor="#21dbd4"
        android:textStyle="bold" />

<GridView 
android:id="@+id/GRIDVIEW1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dip"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
 >  

</GridView>

 <ImageView
    android:id="@+id/trash_can"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/trashcanDescription_Delete"
    android:padding="40dip"
    android:src="@drawable/trashcan"
    android:visibility="gone" >
</ImageView>

</RelativeLayout>
我的gridViewAdapter:

package com.example.awesomefilebuilderwidget;

IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context mContextGV;

// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();

// Constructor
public GridViewAdapter(Context c){
    mContextGV = c;
    Log.d("GridViewAdapter", "Constructor is set");
}

@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
    return drawables.size();
}

@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
    return drawables.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(mContextGV);
        Log.d("GridViewAdapter", "new imageView added");
    }
    drawables.add(R.drawable.pattern1);
    Log.d("GridViewAdapter", "pattern1 added");

    drawables.add(R.drawable.pattern2);
    Log.d("GridViewAdapter", "pattern2 added");

    drawables.add(R.drawable.trashcan);
    Log.d("GridViewAdapter", "trashcan added");

    drawables.add(R.drawable.ic_launcher);
    Log.d("GridViewAdapter", "ic_launcher added");

    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

}
package com.example.awesomefilebuilderwidget;
进口
公共类GridViewAdapter扩展了BaseAdapter{
私有上下文mContextGV;
//将所有图像保留在数组列表中
public ArrayList drawables=new ArrayList();
//建造师
公共GridViewAdapter(上下文c){
mContextGV=c;
Log.d(“GridViewAdapter”,“已设置构造函数”);
}
@凌驾
//此适配器表示的数据集中有多少项
public int getCount(){
返回drawables.size();
}
@凌驾
//获取与中指定位置关联的数据项
//数据集
公共对象getItem(int位置){
返回可抽取项。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//尝试重用视图
ImageView视图=(ImageView)convertView;
//如果convert视图为空,则创建一个新实例,否则重新使用它
如果(视图==null){
视图=新图像视图(mContextGV);
Log.d(“GridViewAdapter”,“新增imageView”);
}
添加(R.drawable.pattern1);
Log.d(“GridViewAdapter”,“添加模式1”);
添加(R.drawable.pattern2);
Log.d(“GridViewAdapter”,“添加模式2”);
添加(R.drawable.trashcan);
Log.d(“GridViewAdapter”,“添加垃圾桶”);
添加(R.drawable.ic_启动器);
Log.d(“GridViewAdapter”,“添加了ic_启动器”);
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER\U裁剪);
view.setLayoutParams(新的android.widget.GridView.LayoutParams(70,70));
view.setTag(String.valueOf(position));
返回视图;
}
}

xml文件中gridview的结束标记

</gridview>

在imageview之前,因此图像将不在gridView中


更一般地说,如果图像不显示,确保您的图像具有所用设备屏幕的分辨率。

imageview用于其他用途…我试图添加到gridView的图像是上面第二个编码块中的pattern1和pattern2,并且模式图像的分辨率应该可以满足要求,我知道,请考虑在你的问题中只输入最小有用的代码。关于你的问题:你应该使用这个绘制来填充一个你添加到布局中的IVIEVIEW。(请参见:)或者,您可以使用可绘制作为布局的背景。(请参阅:)我正在尝试为gridView使用drawable,以便它显示在gridView中。请注意适配器的getView方法,其中我为每张图片创建了一个新的imageView。我将drawables(pattern1和pattern2)添加到名为drawables的ArrayList中,然后使用该ArrayList填充gridView。希望这有助于澄清我试图做的事情。如果你不将你的可绘制列表从
Drag\u and\u Drop\u App
传递到你的adapterOk,那么我应该如何(或者更好的问题是在哪里)添加可绘制列表?在适配器中?或者我应该更改列表的位置吗?如果不需要重用适配器,我建议在适配器中初始化列表。好的,所以我将适配器从拖放类中取出,并将内容放入适配器中(请检查上面更新的问题),但显然,没有调用用于将图片设置到gridView中的我的日志。请在适配器的构造函数中添加这些可绘图项的ID。否则,
getCount()
返回0,而
getView()
永远不会被调用,因为gridview认为有0项。
 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);

    // GridView
    Log.d("D&D", "onCreate called");

    android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);

    // Instance of Adapter Class
    gridView.setAdapter(new GridViewAdapter(this));
   // gridView.setOnItemLongClickListener(this);
package com.example.awesomefilebuilderwidget;

IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context mContextGV;

// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();

// Constructor
public GridViewAdapter(Context c){
    mContextGV = c;
    Log.d("GridViewAdapter", "Constructor is set");
}

@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
    return drawables.size();
}

@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
    return drawables.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(mContextGV);
        Log.d("GridViewAdapter", "new imageView added");
    }
    drawables.add(R.drawable.pattern1);
    Log.d("GridViewAdapter", "pattern1 added");

    drawables.add(R.drawable.pattern2);
    Log.d("GridViewAdapter", "pattern2 added");

    drawables.add(R.drawable.trashcan);
    Log.d("GridViewAdapter", "trashcan added");

    drawables.add(R.drawable.ic_launcher);
    Log.d("GridViewAdapter", "ic_launcher added");

    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

}
</gridview>