如何避免的ImageView和GridView中的LinearLayout之间的空间空间、ImageView、GridView、LinearLayout

2023-09-08 08:35:24 作者:捅万刀

我在我的.xml文件中的图像视图,网格视图。当我执行这个文件的获取网格视图和图像视图之间的一些空间。如何避免这个空间......任何一个可以帮助我解决这个问题。 提前致谢... 我的XML code是:     

I have a image view and grid view in my .xml file. When I am executing this file its getting some space between grid view and image view. How to avoid this space...can any one help me to solve this problem. thanks in advance... my xml code is:

    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>

推荐答案

您应该尝试设置安卓重力正确的属性,并且有工作:

You should try setting the android:gravity attribute correctly, and it has to work:

<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>

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

i'm using here simple color background, and there is no gap:

如果您仍然有问题,那么你应该会看到你的 blue_bg_with_text2 形象,是否有任何空像素的底部。

If you still having the problem, then you should see your blue_bg_with_text2 image, whether it has any empty pixels at the bottom.

PS:忽略安卓了minWidth =250dp机器人:了minHeight =30dp部分,它的存在模仿与尺寸的图像。

ps: ignore the android:minWidth="250dp" android:minHeight="30dp" part, it is there to imitate an image with that size.

更新

下面是使用RelativeLayout的同样的看法(它是一个更清楚一点,至少对我来说):

Here is the same view using RelativeLayout (it's a bit clearer at least for me):

<?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>

我添加到它我的资源作为背景(其中两个实际上是从你的截图)。 我所观察到的是,你必须

I've added to it my resources as background (two of them are actually screenshots from yours). What i observed is, you must

要么使用一个静态的形象为您 日历标题背景,或者, 如果您使用XML绘制,你有 从中删除笔划元/套的机器人:宽度 0 either use a static image for your calendar header background, or, if you use an xml drawable, you have to drop the stroke element from it / set its android:width to 0

否则会增加不必要的差距在你的视图的绘制,这是一样宽,您指定的笔画宽度。

otherwise it will add an unnecessary gap around your view's drawable, that is as wide as the stroke width you've specified.

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

my output using static images as backgrounds looks this way:

更新2

的问题是不是你的日历头和GridView的,但你的GridView里面。 它具有5像素内的填充(不是真正的填充,看看它是如何到达那里):

The problem is not between your calendar header and gridView, but inside your gridView. It has a 'padding' of 5 px inside (not real padding, see how it gets there):

默认选择的GridView 是一个9x9的补丁矩形5px的的边框。 这5 PX可见各地你的GridView。 您可以通过XML或者指定一个不同的listSelector此格:

The default selector for the GridView is a 9x9 patch rectangle with a border of 5px. That 5 px is visible all around your gridView. You can specify a different listSelector for this grid either by xml:

    android:listSelector="@drawable/grid_selector"

或code(的onCreate):

or from code (onCreate):

    gridView.setSelector(R.drawable.grid_selector);

现在,如果你的grid_selector是一个形象,或者是可拉伸的XML没有任何利润,那么引起该问题消失了国内空白。

Now if your grid_selector is an image or is a drawable xml without any margins, then the gap that causes the problem is gone.

您也可以隐藏的差距,用背景色设置为你的GridView,匹配或grandients日历头的背景色。

You can also hide that gap, by setting a background color to your gridView, that matches or grandients the calendar header's background color.