首页 > 解决方案 > LinearLayout 项目不会改变重力

问题描述

我有以下布局:

<LinearLayout 
    orientation="horizontal" 
    layout_width="match_parent" 
    layout_height="match_parent">

    <ImageView 
            layout_width="50dp" 
            layout_height="match_parent" 
            srcCompat="@drawable/eshop" 
            id="@+id/imageView" 
            contentDescription="@string/eshop_icon_description"/>

    <TextView 
            id="@+id/game_price" 
            text="@string/game_price" 
            layout_margin="5dp" 
            layout_width="wrap_content" 
            layout_height="wrap_content" 
            textColor="#2196F3"/>

    <TextView 
            id="@+id/game_discount" 
            text="@string/game_discount" 
            layout_marginTop="5dp" 
            layout_marginEnd="5dp" 
            layout_marginRight="5dp" 
            layout_width="wrap_content" 
            layout_height="wrap_content" 
            textColor="#E91E63"/>
</LinearLayout>

这是它的外观:

在此处输入图像描述

我希望价格和折扣位于右侧,徽标位于左侧,但它们不会使用重力布局或重力改变。

我错过了什么?

标签: androidlayoutgravity

解决方案


让你LinearLayout定位vertical而不是horizontal

然后使用android:layout_gravity="left"android:layout_gravity="right"


推荐阅读