0

今天我正在处理布局,我有一个问题,我认为是基本的。

我有一个线性布局和一个图像视图。我想在布局的左侧或右侧显示图像

我试过了,layout_gravity 重要的是要准确地说我想在线性而不是相对或其他东西上做。

预先感谢,一个屏幕可以准确我的问题

http://hpics.li/fc67805

我的线性与图像

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

            <ImageView
                android:id="@+id/Reboot"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.3"
                android:layout_gravity="left"
                android:src="@drawable/arreter" />
</LinearLayout>
4

4 回答 4

1

发生的事情是您ImageView正在“匹配” parent 的大小LinearLayout,因此无法将其对齐到左侧或右侧......在这种情况下毫无意义,因为无论如何视图都会占用整个空间。如果您真的想可视化图像与其父级的左侧或右侧对齐的效果,您可以将layout_widthand更改layout_height为如下...ImageViewwrap_content

     <ImageView
            android:id="@+id/Reboot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:src="@drawable/arreter" />

另外,请注意我删除了该layout_weight属性,因为它对于 a 中的单个子视图也无用LinearLayout

于 2014-07-29T08:20:37.450 回答
0

使用此布局将 ImageView 对齐到右/左

<?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="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/Reboot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_gravity="left"         // you can use right also. It works.
                android:src="@drawable/ic_launcher" />
</LinearLayout>

您的 LinearLayout 方向是horizontal,因此您可以将子视图放置在top and bottomusing 中android:layout_gravity。对于horizontalLinearLayoutright and left不起作用,它们仅适用于verticalLinearLayout。希望这些信息有用:)

于 2014-07-29T08:34:16.417 回答
0

试试这个方法,希望这能帮助你解决你的问题。

到左侧

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

        <ImageView
            android:id="@+id/Reboot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher" />
</LinearLayout>

到右侧

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">

        <ImageView
            android:id="@+id/Reboot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher" />
</LinearLayout>
于 2014-07-29T08:43:27.940 回答
0

这个问题可能已经回答了,但万一其他人发现了这个

Layout_gravity 和 Gravity 是不同的。Layout_gravity 更改项目在其 ViewGroup 中的位置,而 andorid:gravity 更改项目内容的位置。

关于这里的区别有一个很好的讨论

现在,由于 LinearLayout 的宽度和 ImageView 的宽度都是 match_parent,layout_gravity 将不起作用,因为它们已经是相同的大小。您需要将 LinearLayout 的宽度保持在match_parent并将 ImageView 的宽度更改为wrap_content。然后,您可以使用android:layout_gravity ="right" 或android:layout_gravity ="left" 将图像向右或向左移动。

注意:这也适用于向上/向下移动图像。只需更改 ViewGroup 和 ImageView 的高度而不是宽度并使用 android:layout_gravity="top" 或 android:layout_gravity="bottom"

于 2017-09-23T20:05:37.193 回答