Android 将ImageView与其父布局(即线性布局)右侧对齐

Android 将ImageView与其父布局(即线性布局)右侧对齐,android,android-layout,Android,Android Layout,我想将ImageView与其父布局的右端对齐,这是强制性的LinearLayout。也不建议在任何地方使用RelativeLayout。如下图所示,突出显示的图像应位于屏幕右侧。并且没有使用RelativeLayout。提前谢谢 下面是我的Activity_Main.xml代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schem

我想将ImageView与其父布局的右端对齐,这是强制性的LinearLayout。也不建议在任何地方使用RelativeLayout。如下图所示,突出显示的图像应位于屏幕右侧。并且没有使用RelativeLayout。提前谢谢

下面是我的Activity_Main.xml代码

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4"
    tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/img_back_description"
            android:src="@drawable/back_icon" />

        <TextView
            android:id="@+id/txt_app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txt_app_name" />

        <ImageView
            android:id="@+id/img_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/img_menu_description"
            android:src="@drawable/menu_icon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:weightSum="2" >

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed_mirror"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/txt_view_top" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_label_gallery" />
    </LinearLayout>

</LinearLayout>

假设在水平布局中有三个视图ImageView、TextView、ImageView,则可以使用以下代码:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="prova"
            android:textSize="30sp"
            android:layout_weight="1"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/three_dots"/>
</LinearLayout>

可以在LinearLayout中的ImageView之前插入一个不可见视图,该视图将占用所有可用空间,如下所示:

<View
     android:layout_width="0dp"
     android:layout_height="1dp"
     android:layout_weight="1"/>
您可以像这样使用它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4"
    tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/img_back_description"
            android:src="@drawable/back_icon" />

        <TextView
            android:id="@+id/txt_app_name"
            android:layout_width="match_content"
            android:layout_height="wrap_parent"
            android:drawableRight="@drawable/menu_icon"
            android:text="@string/txt_app_name" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:weightSum="2" >

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed_mirror"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/txt_view_top" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_label_gallery" />
    </LinearLayout>

</LinearLayout>

我试过这个代码。只需使用以下代码更改您的textview txt_app_名称

<TextView
        android:id="@+id/txt_app_name"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/txt_app_name" />

试着这样做,并改变你想说的。 改变布局和重量,直到你没有得到你想要的。我希望这会对你有更多帮助

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:contentDescription="@string/img_back_description"
        android:src="@drawable/back_icon" />

    <TextView
        android:id="@+id/txt_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/txt_app_name" />

    <ImageView
        android:id="@+id/img_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:contentDescription="@string/img_menu_description"
        android:src="@drawable/menu_icon" />
</LinearLayout>
快乐编码

试试这个

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4"
tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/img_back_description"
        android:src="@drawable/back_icon" />

    <TextView
        android:id="@+id/txt_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_app_name" />
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
               <ImageView
                  android:id="@+id/img_menu"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:contentDescription="@string/img_menu_description"
                  android:src="@drawable/menu_icon" />
       </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:orientation="horizontal"
    android:weightSum="2" >

    <com.PinchZoom.pinchzoomexampletwo.TouchImageView
        android:id="@+id/img_to_be_zoomed"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/img" >
    </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

    <com.PinchZoom.pinchzoomexampletwo.TouchImageView
        android:id="@+id/img_to_be_zoomed_mirror"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/img" >
    </com.PinchZoom.pinchzoomexampletwo.TouchImageView>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/txt_view_top" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.25"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_label_gallery" />
</LinearLayout>


Oopps…抱歉…我的错误…忘记发布…@NileshJaradplace在textView和imageView之间放置权重为1的空视图。无论如何,这是一个丑陋的解决方法,你应该使用RelativeLayout,因为它是为此目的创建的。你说得对,但我正在接受培训,想做实验。我的上级不允许我使用relative layout。我也研究过谷歌,但没有找到任何解决方案。所以,我必须在这里提出一个问题。@VladMatvienkoI已经发布了答案,它正在工作。请检查一下,您似乎正在尝试创建一个工具栏,为什么不直接使用实际的工具栏呢?它不会产生期望的结果。你检查了吗?@Kinnar VasaSeems我很好,我已经添加了虚拟图像作为复选框,带有勾号:你不能合并应用程序名称和菜单图标。这是不可取的。这看起来像是一个补丁。@Kinnar VasaIt是默认的android api,而不是补丁:它就像一个补丁,而不是一个真正的解决方案。@Azertiti如果他想使用LinearLayout和TextView来获得一个按下状态,这可能是最合理的。其他选项是使TextView占据整个空间,但按下它时会反映出这一点,或者使ImageView占据可能在缩放时遇到麻烦的空间。考虑到这两种解决方案的缺点,我选择看不见的视图。它说嵌套权重对性能有害。你的解决方案是有效的,但给出了警告。不好吗?@bunny从父线性布局中移除权重。你可以做的另一件事是:在你的三个视图周围添加另一个线性布局。这是一个完美的答案,兄弟@JaydipThis不是正确答案@MaulikM.Dodia:视图的设置权重将三个视图的水平空间分别划分为25%、50%、25%。也许对你来说很好,但这不是你问题的答案。我是Android的新手,你的答案也很完美,但这两个答案都给了我渴望的结果,我看不到任何通往答案的道路。如我所知,如果您指定任何孩子的体重,您必须为每个线性布局孩子指定布局权重。所以,我接受了杰迪普的答案。@joseph82没问题@MaulikM.Dodia,我只是说这个答案解决了一个与你的答案稍有不同的问题;