English 中文(简体)
Android GridGridView 缺陷, 如何将额外的白色空间移到右边
原标题:Android GridView imperfection, how to remove extra white space to the right

我有一个基于 GridView 的日历。 我有以下的 XML 版式, 由选择器设置为空, 因此 < code> 和 mid: listSelector="@null" 。 根据我从这个网站得到的建议。 现在我要从 GridView 右侧获得一些宽的像素条。 为什么? 我已尝试了所有可以使用的方法 。 以下是我的 XML 版式 :

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

            <GridView
                android:id="@+id/calendar"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:horizontalSpacing="-1px"
                android:numColumns="7"
                android:stretchMode="columnWidth" 
                android:gravity="center"
                android:verticalSpacing="-1px"
                android:listSelector="@null" >

            </GridView>

        </LinearLayout>

我得到的是这张照片:

""https://i.sstatic.net/1IDMV.png" alt="此处输入图像描述"/"

最佳回答

This space is due to imperfect calculation for each row of your grid. For example your device width is 320 px and you have 7 rows, try any calculation that meets 320 px. If the width of each cell is 45.71428571428571 px, only then it can be reduced.

其他选项

apply android:gravity="center" property in you grid so that spaces will equally divided from left to right

问题回答

以我的情况来看,我有一个水平间距 1DP

android:horizontalSpacing="1dp"

为了解决这个问题,我把正确的垫子放成 -1dp

android:paddingRight="-1dp"

虽然我原本希望左侧有位子,

尝试使用

android:layout_margin="0dp"
android:padding="0dp"

Also you might have that space allocated for the scroll bar.
And why do you have your vertical and horizontal spacing with negative values?

I had the same problem though in my case I didn t have control over the GridView instance so I couldn t read the column width. Nevertheless I could subclass its container.
This isn t very orthodox but you can override the onMeasure method and add a few pixels.
Something like this:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int RIGHT_MARGIN_OVERSIZE_DIP = 6;
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = this.getMeasuredWidth()
    int overSize = (int)((getResources().getDisplayMetrics().density*RIGHT_MARGIN_OVERSIZE_DIP) + 0.5f);
    widthMeasureSpec = MeasureSpec.makeMeasureSpec(width + overSize, MeasureSpec.getMode(widthMeasureSpec));        
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
grid.setColumnWidth(grid.getColumnWidth()+1);

This will get the current ColumnWidth and add 1 pixel to it. Remember to use this after onCreateView,since the grid needs to be initialized before that.

另外,您的网格视图应该使用相匹配的( 高度和宽度) 。 列中的图像/ 布置也应该与相匹配的相匹配 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签