【问题标题】:How to avoid space between imageview and gridview in linearlayout如何避免线性布局中imageview和gridview之间的空间
【发布时间】:2011-04-16 04:49:18
【问题描述】:

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

【问题讨论】:

  • 您将这两个布局用作父级?请你分享一下,看看他们的属性吗? - 为了避免一个真正好的答案最终会弄乱你的布局。
  • 我正在使用 Linearlayout...我已经用代码更新了问题。

标签: android android-xml


【解决方案1】:

你应该尝试正确设置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 可绘制对象,您有 从中删除笔画元素/将其android:width 设置为 0

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

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

更新 2

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

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

    android:listSelector="@drawable/grid_selector"

或来自代码(onCreate):

    gridView.setSelector(R.drawable.grid_selector);

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

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

【讨论】:

  • 它不适合我。而且我的图像中没有空像素。
  • 那也请分享一个截图,也许我们就能弄清楚那里发生了什么。您的 GridView 是否也有背景图像/可绘制对象?否则你怎么知道差距? (这就是为什么我也为网格应用了背景)
  • 我有屏幕截图,但如何将其添加为评论或其他内容。你能把你的邮件地址发给我,然后我可以把我的截图寄给你
  • 你应该编辑你的问题,点击add image按钮,然后上传它。然后它将成为您问题的一部分。但是,如果您有指向您的截图的链接,那也没关系。
  • 4shared.com/photo/wtGIqbOl/Picture1.html 这是屏幕截图的链接
猜你喜欢
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
相关资源
最近更新 更多