Java gridview项目之间的完美线性间距

Java gridview项目之间的完美线性间距,java,android,android-layout,Java,Android,Android Layout,我使用以下代码来显示GirdView项 <GridView android:id="@+id/gridView" android:gravity="center_horizontal" android:layout_below="@+id/searchLayout" android:layout_width="match_parent" android:layout_he

我使用以下代码来显示GirdView项

<GridView
            android:id="@+id/gridView"
            android:gravity="center_horizontal"
            android:layout_below="@+id/searchLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:horizontalSpacing="10dp"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

每个网格项(ImageView)的大小
92dp

我想要的是每行只显示3列或3个图像,并且每个顶部、底部、左侧和右侧都需要完全对齐和相等

下面是上述代码的结果

可以看出,和图像之间和行之间的空间相比,网格左侧和右侧的空间非常小

其次,我使用的是92dp。以上是S3的结果,但当我使用小屏幕时,第三张图像不像320 dp屏幕那样合适


使用“dp”是否应该根据屏幕大小自动调整?

我想这段代码会对您有所帮助 试试看:-

GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
android:gravity="center"
android:stretchMode="columnWidth"
android:background="#000000"/>
在GridView的xml中

    <GridView 
        android:id                = "@+id/gridViewSms"
        android:layout_width      = "fill_parent"
        android:layout_height     = "fill_parent"
        android:numColumns        = "3"
        android:horizontalSpacing = "10dp"
        android:verticalSpacing   = "10dp"
        android:gravity           = "center"
        android:stretchMode       ="columnWidth">
在你的活动中

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MainActivity  extends Activity{


    private Integer[] mThumbIds = {
            R.drawable.image1 ,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
            R.drawable.image7,
            R.drawable.image8,
            R.drawable.image9
    };


    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        //found width of Screen for Gridview
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        int densityX = display.getWidth();



        GridView grid = (GridView) findViewById(R.id.gridViewSms);
        grid.setAdapter(new ImageAdapter(getApplicationContext(), densityX, mThumbIds));

        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position ,
                    long id) {


            }
        });


    }

}
导入android.app.Activity;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.Display;
导入android.view.view;
导入android.view.WindowManager;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.GridView;
公共类MainActivity扩展了活动{
私有整数[]mThumbIds={
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
R.drawable.image9
};
@抑制警告(“弃用”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到Gridview的屏幕宽度
WindowManager WindowManager=(WindowManager)getSystemService(Context.WINDOW\u服务);
Display Display=windowManager.getDefaultDisplay();
int densityX=display.getWidth();
GridView grid=(GridView)findViewById(R.id.gridViewSms);
setAdapter(新的ImageAdapter(getApplicationContext(),densityX,mThumbIds));
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共控件单击(AdapterView父对象、视图v、内部位置、,
长id){
}
});
}
}

我测试过了。它可以工作。

在过去,我使用了
GridLayout
而不是
GridView
,这样我就可以保持与api v8的兼容性

对于GridLayout,我编写了一个漂亮的小函数,放置在activity中,用于动态拉伸/分隔网格中的项目,以均匀填充视图。我在这里写到:

为了您的方便,我还复制了以下代码:

public void fillview(android.support.v7.widget.GridLayout gl)
{
    Button buttontemp;

    //Stretch buttons
    int idealChildWidth = (int) ((gl.getWidth()-20*gl.getColumnCount())/gl.getColumnCount());
    for( int i=0; i< gl.getChildCount();i++)
    {
        buttontemp = (Button) gl.getChildAt(i);
        buttontemp.setWidth(idealChildWidth);
    }
}
这必须由观察者完成,因为在调用视图之前,我们需要等待绘制视图。

请尝试以下代码:

Grid.xml:

<GridView
        android:id="@+id/photo_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:numColumns="3" />

用于照片的自定义xml文件:

<ImageView
        android:id="@+id/img_photo"
        android:layout_width="@dimen/grid_photo"
        android:layout_height="@dimen/grid_photo"
        android:scaleType="fitXY"
        android:layout_margin="5dp"
        android:src="@drawable/ic_launcher" />

注意:在这里,我建议使用不同尺寸的图像,而不是固定的92dp。所以,通过使用dimens.xml,您可以在Gridview中从四面八方获得不同分辨率的精确宽度和高度

我很久以前也遇到过同样的问题&通过 这:

您可以在中进行一些更改属性,如下所示 Gridview:

<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/rltop"
        android:fadingEdge="none"
        android:fitsSystemWindows="true"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:soundEffectsEnabled="true"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>
最重要的是:

在GridAdapter类->的getView()方法中:

调用下面的方法,以不同分辨率的设备为目标:

    public void screenResolutions(int screen_width,int screen_height, RelativeLayout relativeLayout)
         {
            if((myapp.screen_width == 240)&&(myapp.screen_height==320))
            {
    //          relativeLayout.getLayoutParams().height=(myapp.screen_height-(35+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
            }
            else if((myapp.screen_width== 720)&&(myapp.screen_height==1280))
            {
      //        relativeLayout.getLayoutParams().height=(myapp.screen_height-(107+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width== 320)&&(myapp.screen_height==480))
            {
  //            relativeLayout.getLayoutParams().height=(myapp.screen_height-(47+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 480)&&(myapp.screen_height==800))
            {
//              relativeLayout.getLayoutParams().height=(myapp.screen_height-(71+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 768)||(myapp.screen_height==1280))
            {
  //            relativeLayout.getLayoutParams().height=(myapp.screen_height-(114+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 540)&&(myapp.screen_height==960))
            {
    //          relativeLayout.getLayoutParams().height=(myapp.screen_height-(80+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 1080)||(myapp.screen_height==1920))
            {
   //           relativeLayout.getLayoutParams().height=(myapp.screen_height-(160+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
         }
其中relativelayout=由ImageView组成的布局

您还必须将imageview的高度和宽度更改为目标 多种分辨率如下:

将其放在您的imageview中:

    android:layout_width="@dimen/image_width"
    android:layout_height="@dimen/image_height"
这样说:

在values文件夹中values hdpi=>dimens.xml(480x800)

92dp
92dp
在values文件夹中values mdpi=>dimens.xml(320x480)

64dp
64dp
在values文件夹values-sw360dp-notlong-hdpi=>dimens.xml(540x960)中

114dp
114dp
在values文件夹values-sw360dp-xhdpi=>dimens.xml(720x1280)中

138dp
138dp
在values文件夹values-sw360dp-notlong-xhdpi=>dimens.xml(768x1280)中

184dp
184dp

您是否尝试过由提供的默认演示

此演示清楚地显示了所有高度和宽度相同且周围空间相等的图像

请也检查一下这个

如果这对你没有帮助,请告诉我


享受编码……:)

@Sager我不知道为什么在所有的dimen都按照dimens.xml设置时使用screenResolutions()。其次,如果我使用上述代码,我可以在水平方向上提供相等的空间,但在垂直方向上它们的空间不相等equal@MuhammadUmar什么决议这样做是:如果您使用的是分辨率,比如480x800,那么只有3个图像可以完全恢复您的整个设备宽度,即480。这意味着您的设备宽度分为3个部分。是的,但垂直距离不是线性的。水平方向可以。对于垂直方向,您也可以通过在sreeresolution()方法中取消注释我的高度代码来完成相同的操作。您尝试过我的答案吗?如果你的问题没有解决,请告诉我。
Display  display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        myapp.screen_width = display.getWidth(); 
        myapp.screen_height = display.getHeight();
    public void screenResolutions(int screen_width,int screen_height, RelativeLayout relativeLayout)
         {
            if((myapp.screen_width == 240)&&(myapp.screen_height==320))
            {
    //          relativeLayout.getLayoutParams().height=(myapp.screen_height-(35+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;
            }
            else if((myapp.screen_width== 720)&&(myapp.screen_height==1280))
            {
      //        relativeLayout.getLayoutParams().height=(myapp.screen_height-(107+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width== 320)&&(myapp.screen_height==480))
            {
  //            relativeLayout.getLayoutParams().height=(myapp.screen_height-(47+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 480)&&(myapp.screen_height==800))
            {
//              relativeLayout.getLayoutParams().height=(myapp.screen_height-(71+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 768)||(myapp.screen_height==1280))
            {
  //            relativeLayout.getLayoutParams().height=(myapp.screen_height-(114+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 540)&&(myapp.screen_height==960))
            {
    //          relativeLayout.getLayoutParams().height=(myapp.screen_height-(80+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
            else if((myapp.screen_width == 1080)||(myapp.screen_height==1920))
            {
   //           relativeLayout.getLayoutParams().height=(myapp.screen_height-(160+General.getStatusBarHeight(cntxt)))/3;
                relativeLayout.getLayoutParams().width=(myapp.screen_width)/3;


            }
         }
    android:layout_width="@dimen/image_width"
    android:layout_height="@dimen/image_height"
<dimen name="image_width">92dp</dimen>
<dimen name="image_height">92dp</dimen>
<dimen name="image_width">64dp</dimen>
<dimen name="image_height">64dp</dimen>
<dimen name="image_width">114dp</dimen>
<dimen name="image_height">114dp</dimen>
<dimen name="image_width">138dp</dimen>
<dimen name="image_height">138dp</dimen>
<dimen name="image_width">184dp</dimen>
<dimen name="image_height">184dp</dimen>