2012-05-23 34 views
5

我有一個基於GridView的日曆。我有以下的XML佈局選擇器設置爲空,因此android:listSelector="@null"根據我從這個網站得到的建議。現在,我在GridView的右側獲得了幾個像素寬的條帶。爲什麼?我已經嘗試了所有可以利用的東西。這裏是我的XML佈局:Android GridView不完美,如何刪除多餘的空白到右邊

 <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> 

我得到的是這樣的畫面:

enter image description here

+0

也許這是一個滾動條的問題?嘗試將滾動條可見性設置爲false。 – Demonick

+0

您是否嘗試將水平間距從-1更改爲0? – matt5784

+1

@Demonick,我試過這個滾動條問題,它沒有工作。看到'dinesh sharma's'答案下面它似乎是有道理的。 – Lukuluba

回答

6

此空間是由於您的網格的每一行的計算不完美。 例如,您的設備寬度爲320像素,並且您有7行,請嘗試任何滿足320像素的計算。如果每個單元格的寬度是45.71428571428571 px,那麼只能減少它。

其他選項

申請 安卓重力=「中心」在您電網 屬性,使空間將同樣從左至右

+0

Yah,you是對的@ dinesh-sharma。在橫向模式下,差距幾乎消失,證明問題與屏幕尺寸有關。我沒有得到'android:gravity =「center」'工作的東西。但是,這看起來相當微不足道。謝謝! – Lukuluba

+0

我試過'android:gravity =「center」'和'android:layout_gravity =「center」'解決方案,他們沒有工作。我有一個妥協解決方案,在代碼中指定列寬,使它們的組合寬度大於devince的屏幕寬度。這迫使列填滿屏幕,但最後一列稍微修剪 - 如果因爲邊框消失而有邊框,則不太好。 with是這樣指定的:[link] http://bytecapsule.blogspot.com/2011/08/android-empty-space-to-right-of.html(ByteCapsule) – Lukuluba

0

嘗試使用

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

您可能還需要分配滾動欄空間。
爲什麼你的垂直和水平間距都是負值?

0

分我有同樣的問題,但在我的情況我沒有控制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); 
} 
3

在我的情況下,我不得不水平間距爲1DP

android:horizontalSpacing="1dp" 

所以要解決這個我只是把右填充爲-1dp

android:paddingRight="-1dp" 

即使由於這個原因,我預計會在左側獲得一個空間,但它工作正常

+0

我有horizo​​ntalSpacing =「0dp」,這個解決方案無論如何幫助我。空的空間消失了。 –

+1

@Rempelos如果我沒有記錯的話,paddingRight中的數字應該是-horizo​​ntalSpacing,所以如果你有一個2dp的horizo​​ntalSpacing,那麼你的paddingRight應該是-2 – Jimmar

+1

@JiMMaR其實你是正確的。我把這個例子與我的'horizo​​ntalSpacing =「0dp」'混淆了,並且仍然有額外的空間出現在右邊,但只加了'paddingRight =「 - 1dp」'就修正了它。 – Rempelos

0
grid.setColumnWidth(grid.getColumnWidth()+1); 

這將得到當前的ColumnWidth併爲其添加1個像素。 請記住在onCreateView之後使用它,因爲在此之前需要對網格進行初始化。

此外,你的網格視圖,應該使用match_parent(高度和寬度)。列中的圖像/佈局也應該是match_parent。