修改代码让ImageView在LinearLayout中居左显示
时间: 2023-06-08 10:07:19 浏览: 51
您可以使用以下代码将 ImageView 放置在 LinearLayout 左侧:
```
<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/image"
android:layout_gravity="start" />
</LinearLayout>
```
此代码中, `layout_gravity` 属性被设置为 "start",这意味着 ImageView 将被放置在 LinearLayout 左侧。 如果您想要将 ImageView 放置在 LinearLayout 右侧,则将 `layout_gravity` 属性设置为 "end"。
相关问题
修改代码让ImageView在LinearLayout中居左显示: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/local" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10sp" android:text="03月31日 10:51发布" /> <ImageView android:id="@+id/like" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10sp" android:layout_gravity="end" android:background="@drawable/ic_likes" /> </LinearLayout>
可以将 ImageView 的 layout_gravity 属性改为 "start",代码如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/local"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@ id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:text="03月31日 10:51发布" />
<ImageView
android:id="@ id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10sp"
android:layout_gravity="start"
android:background="@drawable/ic_likes" />
</LinearLayout>
这样就可以让 ImageView 在 LinearLayout 中居左显示了。
修改代码让ImageView在LinearLayout中居右显示,但是使用 android:layout_gravity="end" 没作用
你可以尝试使用android:layout_alignParentRight="true",或者将ImageView放在一个RelativeLayout中,并设置 android:layout_alignParentRight="true",这样ImageView就能在LinearLayout中居右显示了。