【发布时间】:2018-03-11 18:33:32
【问题描述】:
将 Gravity 属性应用于我的 XML 项目后,它们似乎没有放置在正确的位置。如何解决这些问题,以便满足以下条件?:
- “上移”按钮需要位于屏幕顶部
- “下移”按钮需要位于屏幕底部
-
GridView需要位于屏幕的垂直中心
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_moveup"
android:text="move up"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="top"
/>
<GridView
android:id="@+id/abslistview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:numColumns="auto_fit"
/>
<Button
android:id="@+id/btn_movedown"
android:text="move down"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="bottom"
/>
</LinearLayout>
更新(akshay_shahane 的建议)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_moveup"
android:text="move up"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="5"
android:onClick="moveup_click"
/>
<GridView
android:id="@+id/abslistview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="90"
android:layout_gravity="center_vertical"
android:numColumns="auto_fit"
/>
<Button
android:id="@+id/btn_movedown"
android:text="move down"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="5"
android:onClick="movedown_click"
/>
</LinearLayout>
【问题讨论】:
-
为什么不使用权重?向上移动 0.1,网格 0.8 和向下移动 0.1
-
@akshay_shahane 我做到了,但 GridView 的重要性不断提高!它不会到达垂直中心!
-
尝试将根布局重力设置为 centerVertical?
标签: android xml android-layout android-gridview android-gravity