Android GridView在视图的右侧有额外的间距

时间:2019-07-15 13:49:53

标签: android android-layout gridview layout android-gridview

gridview并不完整match_parent,因为在gridview的右侧有一个白色的小空间。如何删除这个很小的空白?示例:http://prntscr.com/ofa94t

我用于gridview的布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout
    android:id="@+id/layout_days"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#007399"
    android:orientation="horizontal"
    android:paddingTop="4dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:paddingBottom="4dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Zon"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Man"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Din"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Woe"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Don"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_weight="1"
    android:gravity="center"
    android:text="Vrij"
    android:textColor="@android:color/white"
    android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Zat"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

</LinearLayout>



<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gridview"
    android:stretchMode="columnWidth"
    android:gravity="center_horizontal"
    android:numColumns="7"

    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"/>

</LinearLayout>

我的适配器,我可以在其中拉伸视图的高度,但不能拉伸宽度:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    Date monthDate = dates.get(position);
    Calendar dateCalendar = Calendar.getInstance();

    dateCalendar.setTime(monthDate);

    int parentHeight=  parent.getHeight();

    int DayNo = dateCalendar.get(Calendar.DAY_OF_MONTH);
    int displayMonth = dateCalendar.get(Calendar.MONTH) + 1;
    int displayYear = dateCalendar.get(Calendar.YEAR);
    int currentMonth = currentDate.get(Calendar.MONTH) +1;
    int currentYear = currentDate.get(Calendar.YEAR);

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

    if (displayMonth == currentMonth && displayYear == currentYear) {
        view.setBackgroundColor(getContext().getResources().getColor(R.color.green));
    } else {
        view.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    TextView Day_Number = view.findViewById(R.id.calendar_day);
    Day_Number.setText(String.valueOf(DayNo));

    view.setMinimumHeight(parentHeight/6);

    return view;
}

我发现了这个Android GridView imperfection, how to remove extra white space to the right,但似乎没有任何作用。

1 个答案:

答案 0 :(得分:2)

从“网格”视图中删除左,右页边距,       或
您可以制作单独的网格视图单元格布局,使其仅包含一个日历单元格,而其他XML活动包含网格视图,然后从Java类中获取它,如下所示

***** gridview.xml *****

 <GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gridview"
    android:numColumns="7">

</GridView>

**** gridview_layout.xml ****

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lr1"
android:layout_width="match_parent"
android:layout_height="@dimen/margin_45dp"
android:orientation="vertical">

<TextView
    android:id="@+id/calendar_day"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="1"
    android:textColor="@color/primary_black_text"
    android:textSize="13sp"
    android:textStyle="bold">

</TextView>

<TextView
    android:id="@+id/events_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textColor="@color/accent"
    android:textSize="9sp"
    android:textStyle="bold" />

**** Java文件****

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Date monthDate = dates.get(position);
    Date today = new Date(); //*******
    Calendar dateCalendar = Calendar.getInstance();
    dateCalendar.setTime(monthDate);
    int DayNo = dateCalendar.get(Calendar.DAY_OF_MONTH);
    int displayMonth = dateCalendar.get(Calendar.MONTH) + 1;
    int displayYear = dateCalendar.get(Calendar.YEAR);
    View view = convertView;

    if (view == null)
        view = inflater.inflate(R.layout.calendar_cell_layout, parent, false);

    if (monthDate.getMonth() != today.getMonth() || monthDate.getYear() != today.getYear()) {
        TextView myTv = view.findViewById(R.id.calendar_day);
        myTv.setTextColor(getContext().getResources().getColor(R.color.secondary_text));
    } else if (monthDate.getDate() == today.getDate()) {
        TextView myTv = view.findViewById(R.id.calendar_day);
        myTv.setBackground(getContext().getDrawable(R.drawable.roundshape));
        myTv.setTextColor(getContext().getColor(R.color.icons));
    }

    TextView Day_Number = view.findViewById(R.id.calendar_day);
    TextView EventNumber = view.findViewById(R.id.events_id);

    Day_Number.setText(String.valueOf(DayNo));
    Calendar eventCalendar = Calendar.getInstance();
    ArrayList<String> arrayList = new ArrayList<>();

    for (int i = 0; i < events.size(); i++) {
        eventCalendar.setTime(ConvertStringToDate(events.get(i).getDATE()));

        if (DayNo == eventCalendar.get(Calendar.DAY_OF_MONTH)
                && displayMonth == eventCalendar.get(Calendar.MONTH) + 1
                && displayYear == eventCalendar.get(Calendar.YEAR))
        {
            arrayList.add(events.get(i).getEVENT());
            EventNumber.setText(arrayList.size() + " Events");
            EventNumber.setBackgroundColor(000000);
        }
    }
    return view;
}

希望这对您有帮助!谢谢!