2015-07-09 4 views
0

У меня возникла проблема с интервалом в Android при сборке GridView. По сути, я собираю игровое поле 9x9. У меня есть GridView, работающий нормально, но у него есть нежелательное расстояние после каждого квадрата определения по горизонтали (вертикаль в порядке) - поэтому он не подходит к рабочей области должным образом.Нежелательный интервал сетки в Android

EDIT: Кажется, GridView выделил 50dp на квадратную ширину, когда я хочу, чтобы это было 30dp ... не знаете, почему и как я могу его изменить?

Пользовательский код сетки:

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 

public class CustomGrid extends BaseAdapter { 
    private Context mContext; 
    private final int[] Imageid; 


    public CustomGrid(Context c, int[] Imageid) { 
     mContext = c; 
     this.Imageid = Imageid; 
    } 

    @Override 
    public int getCount() { 
     return Imageid.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 
      grid = new View(mContext); 
      grid = inflater.inflate(R.layout.gridsingle, null); 
      ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image); 
      imageView.setPadding(0,0,0,0); 
      imageView.setAdjustViewBounds(true); 
      imageView.setImageResource(Imageid[position]); 
     } else { 
      grid = (View) convertView; 
     } 

     return grid; 
    } 
} 

Общий вид отрывок:

public class Gameplay extends Activity { 
GridView grid; 

    int[] imageId = { 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, 
      R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,R.drawable.square 

    }; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gameplay); 
CustomGrid adapter = new CustomGrid(Gameplay.this, imageId); 
     grid=(GridView)findViewById(R.id.grid); 
      grid.setAdapter(adapter); 
      grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(Gameplay.this, "You Clicked x", Toast.LENGTH_SHORT).show(); 

      } 
     }); 

GridView XML:

<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" 
     tools:context=".MainActivity" > 

     <GridView 
      android:numColumns="9" 
      android:gravity="center" 
      android:columnWidth="30dp" 
      android:stretchMode="none" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:horizontalSpacing="0dp" 
      android:id="@+id/grid" 
      /> 

    </LinearLayout> 

ImageView XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="30dp" 
    android:layout_height="30dp" 
    android:padding="0dp" > 


<ImageView 
     android:id="@+id/grid_image" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:stretchMode="none"> 
    </ImageView> 

</LinearLayout> 

Конечный результат выходит так: test screen

Я пробовал очевидные идеи, как изменения ширины и с использованием параметров StretchMode, но не повезло еще. Есть идеи?

Большое спасибо.

+0

попробуйте установить ширину сетки, чтобы обернуть содержимое – Eoin

+0

К сожалению, это не сработало. Чтобы добавить к моему вопросу, похоже, GridView выделил 50dp ширину на квадрат, когда я хочу, чтобы это было 30dp ... –

ответ

1

Я решил свою проблему - оказалось, что у меня были конфликтующие макеты для моего gridview (сетка ID) - как только я удалил заброшенный макет, все начали работать!

Смежные вопросы