在我的应用程序中,我使用了网格视图,并且在每个网格中都有一个图像。现在我的问题是网格之间的间距非常高。我想减少网格之间的空间。请帮帮我。
在xml布局中我使用了
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
现在我在Xml的每个图像周围都有一个矩形形状
我的xml for gridview。
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"/>
的活动:
public class MainActivity extends Activity {
GridView grid;
RelativeLayout r1;
int maxrows, maxcol;
ArrayList<LayoutData> data = new ArrayList<LayoutData>();
ArrayList<HashMap<String, String>> sheetdetails = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid=(GridView)findViewById(R.id.gridview);
new GetRoute().execute("http://sb2.reloadit.in/TravelServices.asmx/Getlayout");
}
private class GetRoute extends AsyncTask<String, Void, JSONObject> {
/*String mJourneyDate;
public GetData(String pJourneyDate) {
this.mJourneyDate = pJourneyDate;
}*/ProgressDialog pd = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "", "Loading...", true);
}
@Override
protected JSONObject doInBackground(String... params) {
String response;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("RouteScheduleID","434145405"));
nameValuePair.add(new BasicNameValuePair("JourneyDate","2014-12-18"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
response = EntityUtils.toString(httpEntity);
Log.d("response is", response);
return new JSONObject(response);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject result)
{
super.onPostExecute(result);
//Log.v("TAG_RESULTadapter",""+result);
pd.dismiss();
if(result != null)
{
try
{
JSONObject jobj = result.getJSONObject("Response");
String message = jobj.getString("Message");
String issuceess = jobj.getString("IsSuccess");
if(issuceess.equals("true"))
{
JSONObject layout = result.getJSONObject("Layout");
// Log.v("TAG_routearray",""+layoutarray);
maxrows=layout.getInt("MaxRows");
maxcol=layout.getInt("MaxColumns");
grid.setNumColumns(maxcol);
JSONArray routearray = layout.getJSONArray("SeatDetails");
for (int i = 0; i < routearray.length(); i++) {
String Row = routearray.getJSONObject(i).getString("Row");
// Log.v("TAG_routearray",""+Row);
String Col = routearray.getJSONObject(i).getString("Col");
String Height = routearray.getJSONObject(i).getString("Height");
String Width = routearray.getJSONObject(i).getString("Width");
String SeatNo = routearray.getJSONObject(i).getString("SeatNo");
String Gender = routearray.getJSONObject(i).getString("Gender");
String Deck = routearray.getJSONObject(i).getString("Deck");
String IsAvailable = routearray.getJSONObject(i).getString("IsAvailable");
String Fare = routearray.getJSONObject(i).getString("Fare");
LayoutData ld= new LayoutData();
ld.setRow(Row);
ld.setCol(Col);
ld.setHeight(Height);
ld.setWidth(Width);
ld.setSeatno(SeatNo);
ld.setGender(Gender);
ld.setDeck(Deck);
ld.setIsavailable(IsAvailable);
ld.setFare(Fare);
data.add(ld);
Log.v("TAG_Maxfare",""+Fare);
HashMap<String, String> map = new HashMap<String, String>();
map.put("Row", Row);
map.put("Col", Col);
map.put("Height", Height);
map.put("Width", Width);
map.put("SeatNo", SeatNo);
map.put("Deck", Deck);
map.put("IsAvailable", IsAvailable);
if(SeatNo.equalsIgnoreCase("")){
map.put("seat_img", ""+R.drawable.noseat);
}
else{
if(IsAvailable.equalsIgnoreCase("false")){
map.put("seat_img", ""+R.drawable.seatgreen);
}
/* if(IsAvailable.equalsIgnoreCase("false")){
map.put("seat_img", ""+R.drawable.seatgreen);
}*/else if(IsAvailable.equalsIgnoreCase("true")){
map.put("seat_img", ""+R.drawable.seat);
}
}
map.put("Fare", Fare);
sheetdetails.add(map);
}
CustomGrid adapter = new CustomGrid(MainActivity.this, sheetdetails);
grid.setAdapter(adapter);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
Toast.makeText(MainActivity.this, "Network Problem", Toast.LENGTH_LONG).show();
}
}
}
基于json我将行和col添加到网格视图
Customgrid:
package com.app.redbuslayout;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGrid extends BaseAdapter{
private Context mContext;
ArrayList<HashMap<String, String>> sheetdetails = new ArrayList<HashMap<String, String>>();
public CustomGrid(Context c,ArrayList<HashMap<String, String>> sheetdetails) {
mContext = c;
this.sheetdetails= sheetdetails;
}@Override
public int getCount() {
// TODO Auto-generated method stub
return sheetdetails.size();
}@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return sheetdetails.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.row_grid, null);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
HashMap<String, String> map = sheetdetails.get(position);
imageView.setImageResource(Integer.valueOf(map.get("seat_img")));
} else {
grid = (View) convertView;
}
return grid;
}
}
rowgrid:
<LinearLayout 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:padding="5dp"
android:background="#ffffff" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
答案 0 :(得分:0)
您可以使用android:numColumns="any_integer"
来定义一行中的项目数。
例如,您有5个项目,并且希望减少它们之间的水平间距。你应该像android:numColumns="10"
一样。