2016-05-28 159 views
0

我開發了一個應用程序,用戶可以在其中更改背景。我已成功更改它。如何在GridView中顯示圖像

唯一的問題是,當用戶點擊首選項屏幕來更改背景時,它看起來像這樣:http://i.stack.imgur.com/suiRq.png但我希望它在GridView中顯示爲Gallery。

我喜歡的活動代碼:

private static final int[] ChHAT_IMG = new int[]{R.drawable.wallpaper_1, 
     R.drawable.wallpaper_2,R.drawable.wallpaper_3 
     ,R.drawable.wallpaper_4, 
     R.drawable.black_floral_leaf_pattern, 
     R.drawable.niggt,R.drawable.sky_at_night, 
     R.drawable.wallpaper_5,R.drawable.wallpaper_6 
     ,R.drawable.wallpaper_7, 
     R.drawable.wallpaper_9,R.drawable.wallpaper_10 
     ,R.drawable.wallpaper_11,R.drawable.wallpaper_12 
     ,R.drawable.wallpaper_13,R.drawable.wallpaper_16,R.drawable.wallpaper_17,R.drawable.wallpaper_20,R.drawable.wallpaper_21, 

     }; 

/** 
* String resources for wallpaer. 
*/ 
private static final int[] CHAT_STR = new int[]{ 
     R.string.wallpaper1, R.string.wallpaper2, R.string.wallpaper3, R.string.wallpaper4, 
     R.string.wallpaper5, R.string.wallpaper6, R.string.wallpaper7, R.string.wallpaper8, 
     R.string.wallpaper9, R.string.wallpaper10, R.string.wallpaper11, R.string.wallpaper12,R.string.wallpaper13,R.string.wallpaper14, 
R.string.wallpaper15, R.string.wallpaper16, R.string.wallpaper17, R.string.wallpaper18, 
     R.string.wallpaper19, }; 

private static class OnWallpaperClickListener implements 
     Preference.OnPreferenceClickListener { 
    private final Context ctx; 
    public OnWallpaperClickListener(final Context context) { 
     ctx = context; 
    } 

    @Override 
    public boolean onPreferenceClick(final Preference preference) { 

     final Builder b = new Builder(ctx); 
     final int l = CHAT_STR.length; 
     final String[] cols = new String[]{"icon", "text"}; 
     final ArrayList<HashMap<String, Object>> rows 
       = new ArrayList<>(); 
     for (int i = 0; i < l; i++) { 
      final HashMap<String, Object> m = new HashMap<>(2); 
      m.put(cols[0], ChHAT_IMG[i]); 
      m.put(cols[1], ctx.getString(CHAT_STR[i])); 
      rows.add(m); 
     } 
     LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.grid, null); 
     SimpleAdapter adapter=new SimpleAdapter(ctx, rows, R.layout.wallpaper_icons_item, cols, 
         new int[]{android.R.id.icon, android.R.id.text1}); 
     GridView gridView = (GridView) view.findViewById(R.id.gridview); 

     // Setting an adapter containing images to the gridview 
     gridView.setAdapter(adapter); 
    // b.setView(gridView); 
     AlertDialog dialog=b.create(); 
     b.setAdapter(adapter, 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(final DialogInterface dialog, final int which) { 
         preference.getEditor().putInt(preference.getKey(), which).commit(); 
        } 
       }); 
     b.setNegativeButton(android.R.string.cancel, null); 
     b.show(); 
     return true; 
    } 
} 

pref_apperance.xml

<PreferenceScreen 
    android:key="chat" 
    android:summary="Set wallpaper for message"</PreferenceScreen>" 

grid.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:weightSum="1"> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/gridview" 
    android:numColumns="auto_fit" 
    /> 

wallpaper_icons_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
<ImageView 
     android:id="@android:id/icon" 
     android:layout_width="150dip" 
     android:layout_height="150dip" 
     android:gravity="center_vertical" 
     android:padding="5dip" 
     android:scaleType="fitCenter" 
     android:src="@drawable/R.drawable.wallpaper_4"/> 
     <TextView 
     android:id="@android:id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical" 
     android:text="android:id/text1"/></LinearLayout> 

回答

0

創建CoustomAdapter爲您gridview,並設置如以下碼

代碼值=>

public class CustomAdapter extends BaseAdapter{ 

String [] result; 
Context context; 
    int [] imageId; 
    private static LayoutInflater inflater=null; 
public CustomAdapter(MainActivity mainActivity, String[] textList, int[] Imagestext) { 
    // TODO Auto-generated constructor stub 
    result=textList; 
    context=mainActivity; 
    imageId=Imageslist; 
    inflater = (LayoutInflater)context. 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return result.length; 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public class Holder 
{ 
    TextView tv; 
    ImageView img; 
} 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    Holder holder=new Holder(); 
    View rowView; 

     rowView = inflater.inflate(R.layout.program_list, null); 
     holder.tv=(TextView) rowView.findViewById(R.id.textView1); 
     holder.img=(ImageView) rowView.findViewById(R.id.imageView1); 

    holder.tv.setText(result[position]); 
    holder.img.setImageResource(imageId[position]); 

    rowView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show(); 
     } 
    }); 

    return rowView; 
    } 

} 

,並設置在像

主要活性的適配器
gv=(GridView) findViewById(R.id.gridView1);  
    gv.setAdapter(new CustomAdapter(this, textList,Imageslist)); 

可能會幫到你