2016-09-14 73 views
0

我試圖創建一個自定義日曆視圖GridView的孩子沒有延伸到

當我添加TextViews填補了GridView他們不一路擴展到屏幕的邊緣在GridView的邊緣即使一切都設置爲匹配父項。

問題可以在下面看到在模擬器的屏幕的右側: enter image description here

以下是我使用的XML佈局文件:

calendar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <GridView 
     android:id="@+id/calendar_grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/dateBorder" 
     android:verticalSpacing="1dp" 
     android:horizontalSpacing="1dp" 
     android:numColumns="7" 
     /> 

</LinearLayout> 

calendar_day.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/calendar_day" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    android:gravity="right" 
    android:textAppearance="@android:style/TextAppearance.Medium"> 
</TextView> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <niddahcalendar.kosherapps.com.testgridview.MyCalendarView 
     android:id="@+id/my_calendar_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </niddahcalendar.kosherapps.com.testgridview.MyCalendarView> 

</LinearLayout> 

這是我使用的Java代碼:

MyCalendarView.java

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.Calendar; 

public class MyCalendarView extends LinearLayout{ 

    GridView grid; 

    public MyCalendarView(Context context) { 
     super(context); 
     init(context); 
     updateCalendar(); 
    } 

    public MyCalendarView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
     updateCalendar(); 
    } 

    public MyCalendarView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
     updateCalendar(); 
    } 

    private void init(Context context) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.calendar, this); 
     grid = (GridView)findViewById(R.id.calendar_grid); 
    } 

    public void updateCalendar() { 
     final ArrayList<Integer> calendarDays = new ArrayList<>(); 
     Calendar calendar = Calendar.getInstance(); 
     int monthBeginningCell = calendar.get(Calendar.DAY_OF_WEEK) - 1; 
     calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell); 
     while (calendarDays.size() < 42) { 
      int day = calendar.get(Calendar.DAY_OF_MONTH); 
      calendarDays.add(day); 
      calendar.add(Calendar.DAY_OF_MONTH, 1); 
     } 
     CalendarAdapter calendarAdapter = new CalendarAdapter(this.getContext(), calendarDays); 
     grid.setAdapter(calendarAdapter); 
    } 

    class CalendarAdapter extends BaseAdapter { 

     private Context context; 
     private ArrayList<Integer> calendarDays; 

     public CalendarAdapter(Context context, ArrayList<Integer> calendarDays) { 
      this.context = context; 
      this.calendarDays = calendarDays; 
     } 

     @Override 
     public int getCount() { 
      return calendarDays.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return calendarDays.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.calendar_day, parent, false); 
      } 

      //get view 
      TextView textView = (TextView) convertView.findViewById(R.id.calendar_day); 

      //set data 
      textView.setText(String.format("%d", calendarDays.get(position))); 
      textView.setBackgroundColor(getResources().getColor(R.color.dateBackground)); 

      return convertView; 
     } 
    } 
} 

MainActivity.java

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 

public class MainActivity extends AppCompatActivity { 

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

     MyCalendarView myCalendarView = (MyCalendarView) findViewById(R.id.my_calendar_view); 
    } 

} 
+0

奇怪。你確定網格視圖本身延伸到屏幕邊緣嗎?如果將「android:layout_gravity =」right「'屬性添加到網格視圖會發生什麼?間隙是否向左移動?如果你對封閉的'LinearLayout'做同樣的事情呢? –

+0

@TedHopp是的,深灰色是'GridView'的背景顏色。將重力屬性添加到'GridView'和/或'LinearLayout'沒有改變任何東西。 – yitzih

+0

我懷疑這是一個餘數問題:可用寬度不能完全被列數整除。相關問題:http://stackoverflow.com/questions/22643726/adapt-a-gridview-cells-height-to-remaining-space –

回答

0

嘗試增加:

android:stretchMode="columnWidth" 

GridView屬性。

+0

這沒有任何效果 – yitzih