水平/垂直间距无法在Android上正常运行

时间:2019-04-04 19:44:31

标签: android gridview

这个问题使我头痛不已。基本上,我有一个基于GridView的Gallery应用。一切正常。但是我希望图像之间具有水平和垂直相等的间距,因此更容易区分彼此。

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gallery.MainActivity">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:columnWidth="80dp"
        android:scrollingCache="true"
        android:smoothScrollbar="true"
        android:horizontalSpacing="3dp"
        android:verticalSpacing="3dp"
        android:padding="0dp"
        android:clipToPadding="false"
        android:clipChildren="true"
        android:numColumns="auto_fit"
        android:adjustViewBounds="true"
        android:gravity="center"
        android:stretchMode="columnWidth"/>

</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private GridView gridView;


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

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

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getApplicationContext(), FullScreenAnimationAdapter.class);
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }
}

还有ImageAdapter的{​​{1}}:

GetView

因此,我将@Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(ctx); imageView.setImageResource((int)getItem(position)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams(240, 240)); return imageView; } horizontalSpacing都设置为verticalSpacing。但是我的应用程序看起来像这样:

enter image description here

所有图像均为3dp1920x1080

我在做错什么吗?我应该更改xml文件中的某些属性,还是应该为96 dpi使用不同的ScaleType

2 个答案:

答案 0 :(得分:0)

使用带有layout_weight属性的LinearLayout。

sudo代码可帮助您实现这一想法。

<LinearLayout
layout_orientation="vertical">

<LinearLayout
orientation="horizontal">

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />
</LinearLayout>


<LinearLayout
orientation="horizontal">

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />
</LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

尝试以下提及的方式:

In Activity java:

for set space:
  mGridView.setHorizontalSpacing(4);
  mGridView.setVerticalSpacing(4);

for get Spacing:

 mGridView.getHorizontalSpacing()
 mGridView.getVerticalSpacing()


In XML
<GridView
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"     
android:verticalSpacing="10dp">