如何在linearlayout中避免imageview和gridview之间的空间

时间:2011-04-16 04:49:19

标签: android android-xml

我的.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>

1 个答案:

答案 0 :(得分: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>

我在这里使用简单的彩色背景,并且没有差距:

no gap

如果您仍然遇到问题,那么您应该会看到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

否则会在视图的drawable周围添加不​​必要的间隙,这与您指定的笔触宽度一样宽。

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

no gap 2

更新2

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

selector的默认GridView是9x9补丁矩形,边框为5px。
你的gridView周围都可以看到5 px。 您可以通过xml:

为此网格指定不同的listSelector
    android:listSelector="@drawable/grid_selector"

或代码(onCreate):

    gridView.setSelector(R.drawable.grid_selector);

现在,如果你的grid_selector是一个图像,或者是一个没有任何边距的可绘制xml,那么导致问题的差距就会消失。

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