解释android:layout_gravity="start"
时间: 2023-12-04 22:03:27 浏览: 30
android:layout_gravity="start"是一个布局属性,它表示该视图在其容器中水平对齐方式为“开始”(从左到右的开始方向),即从左侧开始。在RTL(Right-to-Left)语言环境中,这会被解释为从右侧开始。这个属性通常用于LinearLayout和FrameLayout等布局容器中。
相关问题
修改代码让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 中居左显示了。
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipeRefresh" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:id="@+id/weatherLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:scrollbars="none" android:visibility="invisible"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include layout="@layout/now" /> <include layout="@layout/forecast" /> <include layout="@layout/life_index" /> </LinearLayout> </ScrollView> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:clickable="true" android:focusable="true" android:background="@color/colorPrimary"> <fragment android:id="@+id/placeFragment" android:name="com.coldweather.android.ui.place.PlaceFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="25dp"/> </FrameLayout> </androidx.drawerlayout.widget.DrawerLayout>
这是一个 Android 布局文件,使用 DrawerLayout 容器来包含一个 SwipeRefreshLayout 和一个 FrameLayout。SwipeRefreshLayout 包含一个 ScrollView,其中包含一个垂直排列的 LinearLayout,该 LinearLayout 包含了三个布局文件:now、forecast、life_index。而 FrameLayout 则包含了一个名为 "placeFragment" 的 Fragment,该 Fragment 的类为 "com.coldweather.android.ui.place.PlaceFragment"。DrawerLayout 的宽度和高度都设置为 match_parent,以填充整个屏幕。SwipeRefreshLayout 和 FrameLayout 的宽度和高度也都设置为 match_parent。FrameLayout 的重力属性设置为 start,意味着它位于屏幕左侧。而 Fragment 设置了一个 marginTop 属性为 25dp,以在顶部留出一些空间。