3

我的 .xml 文件中有一个图像视图和网格视图。当我执行这个文件时,它会在网格视图和图像视图之间获得一些空间。如何避免这个空间......谁能帮我解决这个问题。在此先感谢...我的 xml 代码是:

    android:layout_width="0px"
    android:layout_height="0px">
</Button>

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

    <ImageView
        android:id="@+id/prevMonth"
        android:src="@drawable/cal_left_arrow_off2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
    <Button
        android:id="@+id/currentMonth"
        android:layout_weight="0.6"

        android:textAppearance="?android:attr/textAppearanceMedium"
        android:background="@drawable/calendar_bar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <ImageView
        android:id="@+id/nextMonth"
        android:src="@drawable/cal_right_arrow_off2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
</LinearLayout>
<LinearLayout
    android:layout_gravity="bottom|center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/calendarheader"
        android:src="@drawable/blue_bg_with_text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        >
        </ImageView>
</LinearLayout>
<GridView
    android:id="@+id/calendar"
    android:numColumns="7"
    android:layout_gravity="top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</GridView>

4

1 回答 1

2

您应该尝试android:gravity正确设置属性,并且它必须工作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:gravity="bottom|center_horizontal" 
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <ImageView android:id="@+id/calendarheader" android:src="@color/blue_start"
            android:layout_width="wrap_content" android:layout_height="wrap_content" 
             android:minWidth="250dp" android:minHeight="30dp">
        </ImageView>
    </LinearLayout>
    <GridView android:id="@+id/calendar" android:numColumns="7"
        android:layout_gravity="top" 
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:background="@color/blue_start">
    </GridView>
</LinearLayout>

我在这里使用简单的颜色背景,并且没有间隙:

无间隙

如果您仍然有问题,那么您应该看到您的blue_bg_with_text2图像,无论它的底部是否有任何空像素。

ps:忽略android:minWidth="250dp" android:minHeight="30dp"部分,它在那里模仿具有该尺寸的图像。

更新

这是使用 RelativeLayout 的相同视图(至少对我来说更清晰一些):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:id="@+id/selectedDayMonthYear"
        android:textColor="#000000" android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="0px" android:layout_height="0px" />
    <ImageView android:id="@+id/prevMonth" android:src="@drawable/cal_left_arrow_off2"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <ImageView android:id="@+id/nextMonth" android:src="@drawable/cal_right_arrow_off2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />
    <Button android:id="@+id/currentMonth" android:layout_weight="0.6"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:background="@drawable/calendar_bar2" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/prevMonth"
        android:layout_toLeftOf="@id/nextMonth"
        android:layout_alignBottom="@id/prevMonth"
        android:layout_alignTop="@id/prevMonth" />
    <ImageView android:id="@+id/calendarheader" android:src="@drawable/calendarheader"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:layout_centerInParent="true"
        android:layout_below="@id/currentMonth"
        android:layout_margin="0dp" 
        android:scaleType="fitXY" />
    <GridView android:id="@+id/calendar" android:numColumns="7"
        android:layout_gravity="top" android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/calendarheader"
        android:layout_margin="0dp" 
        android:background="@color/white_fader_end">
    </GridView>
</RelativeLayout>

我已将我的资源作为背景添加到其中(其中两个实际上是您的屏幕截图)。我观察到的是,你必须

  • 为您的日历标题背景使用静态图像,或者,
  • 如果您使用 xml drawable,则必须从中删除笔画元素/将其设置android:width为 0

否则它将在视图的可绘制对象周围添加不​​必要的间隙,该间隙与您指定的笔划宽度一样宽。

我使用静态图像作为背景的输出看起来是这样的:

没有间隙 2

更新 2

问题不在您的日历标题和 gridView 之间,而是在您的 gridView 内部
它内部有一个 5 px 的“填充”(不是真正的填充,看看它是如何到达那里的):

的默认值selectorGridView一个 9x9 的补丁矩形,边框为 5px。
这 5 px 在您的 gridView 周围都是可见的。您可以通过 xml 为该网格指定不同的 listSelector:

    android:listSelector="@drawable/grid_selector"

或来自代码(onCreate):

    gridView.setSelector(R.drawable.grid_selector);

现在,如果您的 grid_selector 是图像或者是没有任何边距的可绘制 xml,那么导致问题的间隙就消失了。

您还可以通过为您的 gridView 设置与日历标题的背景颜色匹配或宏大的背景颜色来隐藏该间隙。

于 2011-04-16T06:11:46.630 回答