我想为电影海报制作一个GridView

时间:2016-12-13 13:30:11

标签: android android-layout

The Layout after running the program我用这两个类创建了一个GridView,我想在屏幕截图中删除图像之间的空间,如何删除这些空格?

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
/>

$ ImageAdapter类:

 public class ImageAdapter extends BaseAdapter{

    Context context;

    public ImageAdapter(Context context){
        this.context=context;

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView imageView;
        if (view == null){
            imageView=new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(120,120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(3,3,3,3);
        }else {
            imageView = (ImageView) view;
        }

        imageView.setImageResource(images[i]);
        return imageView;
    }

    // images
    int [] images={
            R.drawable.one, R.drawable.two,
            R.drawable.three, R.drawable.four,
            /*R.drawable.fastfive, R.drawable.fastsix,
            R.drawable.fastseven*/
    };
}

$ Movies Class:

public class MoviesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies);

        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
    }
}

3 个答案:

答案 0 :(得分:0)

您应该实现如下所示,而不是将高度宽度设置为120, 120。因为app会运行许多不同的分辨率。所以它是终极解决方案

ImageView的宽度为match_parent,高度为(device / 2的宽度)。它将显示方形图像。

示例

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
width = (width / 2);
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT,120));

getView方法中应用此解决方案。

答案 1 :(得分:0)

如果你要结合android:numColumns =&#34; auto_fit&#34;和android:stretchMode =&#34; columnWidth&#34;将为您提供适合不同屏幕尺寸的布局。比修复列数要好得多。见下文。

你也可以使用android:numColumns =&#34; auto_fit&#34;而不是android:numColumns =&#34; 3&#34;这里根据屏幕设置。

<GridView
      android:id="@+id/GVHomepage"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:numColumns="3" 
      android:verticalSpacing="1dp"
      android:horizontalSpacing="1dp"
      android:stretchMode="columnWidth"
      android:background="#edf2f8" />

答案 2 :(得分:0)

另一种方法是创建RecyclerView并设置GridLayoutManager

像这样的东西

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
public class MoviesActivity extends AppCompatActivity {

    private static final int SPAN_COUNT = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies);

        RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
        rv.setLayoutManager(new GridLayoutManager(this, SPAN_COUNT));
        rv.setAdapter(new ImageAdapter(this));
    }
}

适配器项目视图持有者布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>

</RelativeLayout>

这将创建一个带有网格的列表,跨度计数为2.