Android 图像视图不是';使用填充时,请不要垂直对齐

Android 图像视图不是';使用填充时,请不要垂直对齐,android,padding,Android,Padding,我试图得到两个图像视图并排与周围的图像间隙。我一直在使用填充、边距等,所有这些都有相同的效果(见下图) 正如您所看到的,右边的图像向上移动,我如何停止此操作 以下是im使用的布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_par

我试图得到两个图像视图并排与周围的图像间隙。我一直在使用填充、边距等,所有这些都有相同的效果(见下图)

正如您所看到的,右边的图像向上移动,我如何停止此操作

以下是im使用的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Top Row -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/tab_headlines"
            android:src="@drawable/headlines"
            android:layout_margin="5sp"
            />
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/tab_headlines"
            android:src="@drawable/headlines"
            />

    </LinearLayout>


我将使用更多的图像行,因此使用额外的线性布局来固定垂直对齐:

在您的图像视图中使用android:layout\u height=“match\u parent”

通过使用
android:layout\u height=“match\u parent”
inside
ImageView
它将匹配到它的父级
LinearLayout
,即
android:layout\u height=“wrap\u content”

现在问题出现了,为什么两个
图像视图的高度相同

原因由于
LinearLayout中的
android:layout\u Height=“wrap\u content”
,您的父级
LinearLayout的高度将从高度更大的图像视图
中自动选择

通过使用android:layout\u height=“match\u parent”
您将为高度较小的ImageView使用相同的高度


要固定水平对齐:

使用android:layout_weight=“btoh的等重值”

将代码更改为

   <ImageView 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:contentDescription="@string/tab_headlines"
        android:src="@drawable/headlines"
        android:layout_margin="5sp"
        />
    <ImageView 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:contentDescription="@string/tab_headlines"
        android:src="@drawable/headlines"
        />

要固定垂直对齐:

在您的图像视图中使用android:layout\u height=“match\u parent”

通过使用
android:layout\u height=“match\u parent”
inside
ImageView
它将匹配到它的父级
LinearLayout
,即
android:layout\u height=“wrap\u content”

现在问题出现了,为什么两个
图像视图的高度相同

原因由于
LinearLayout中的
android:layout\u Height=“wrap\u content”
,您的父级
LinearLayout的高度将从高度更大的图像视图
中自动选择

通过使用android:layout\u height=“match\u parent”
您将为高度较小的ImageView使用相同的高度


要固定水平对齐:

使用android:layout_weight=“btoh的等重值”

将代码更改为

   <ImageView 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:contentDescription="@string/tab_headlines"
        android:src="@drawable/headlines"
        android:layout_margin="5sp"
        />
    <ImageView 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:contentDescription="@string/tab_headlines"
        android:src="@drawable/headlines"
        />

您为左侧图像提供了版面空白,而没有为右侧图像提供版面空白。这就是问题所在。如果一行中只需要两个图像,并且两个图像的大小相同,则可以使用layout_weight属性并为它们之间的空间提供填充。 所以,试试这个。它会起作用的


您为左侧图像提供了版面空白,而没有为右侧图像提供版面空白。这就是问题所在。如果一行中只需要两个图像,并且两个图像的大小相同,则可以使用layout_weight属性并为它们之间的空间提供填充。 所以,试试这个。它会起作用的


使用
布局\权重
平均分配布局。请尝试以下代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Top Row -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="3dp"
            android:layout_weight="1"
            android:contentDescription="@string/tab_headlines"
            android:src="@drawable/headlines" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="6dp"
            android:layout_weight="1"
            android:contentDescription="@string/tab_headlines"
            android:src="@drawable/headlines" />
    </LinearLayout>
</LinearLayout>


此外,切勿使用
sp
指定布局尺寸。使用
dp
代替。

使用
layout\u weight
平均分配布局。请尝试以下代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Top Row -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="3dp"
            android:layout_weight="1"
            android:contentDescription="@string/tab_headlines"
            android:src="@drawable/headlines" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="6dp"
            android:layout_weight="1"
            android:contentDescription="@string/tab_headlines"
            android:src="@drawable/headlines" />
    </LinearLayout>
</LinearLayout>


此外,切勿使用
sp
指定布局尺寸。改用
dp

对两个图像视图使用
android:layout\u weight=“1”
将修复。对两个图像视图使用
android:layout\u weight=“1”
将修复。