[英]Android GridView imperfection, how to remove extra white space to the right
我有一个基于GridView的日历。 我有以下XML布局,选择器设置为null因此android: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>
我得到的是这张照片:
此空间是由于网格的每一行计算不完善所致。 例如,您的设备宽度为320像素,您有7行,请尝试任何符合320像素的计算。 如果每个单元格的宽度是45.71428571428571 px,那么只有它可以减少。
其他选择
在你的网格中应用android:gravity =“center”属性,以便空格从左到右平分
在我的情况下,我的水平间距为1dp
android:horizontalSpacing="1dp"
所以要解决这个问题,我只需将正确的填充为-1dp
android:paddingRight="-1dp"
尽管由于这个原因我希望在左侧获得一个空间,但它运作正常
尝试使用
android:layout_margin="0dp"
android:padding="0dp"
您也可以为滚动条分配该空间。
为什么你的垂直和水平间距都是负值?
我有同样的问题,但在我的情况下,我没有控制GridView
实例,所以我无法读取列宽。 不过我可以继承它的容器。
这不是很正统,但您可以覆盖onMeasure
方法并添加几个像素。
像这样的东西:
@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);
这将获得当前ColumnWidth并向其添加1个像素。 请记住在onCreateView之后使用它,因为网格需要在此之前初始化。
此外,您的网格视图应使用match_parent(高度和宽度)。列中的图像/布局也应该是match_parent。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.