![](/img/trans.png)
[英]Android - How do I position views in an offset relative to the center/top/bottom (etc.) of their parent?
[英]Center a View in Android between bottom and top views
我需要以某种方式这样做:
我尝试使用相对布局,我放置了顶部的imageview和底部按钮但是如何将我的gridview放在中心? 但是在顶部图像和底部按钮之间?
让顶部图像和底部按钮使用wrap_content
作为高度,GridView使用0dip
作为高度以及权重1.这将导致顶部和底部元素首先测量,并且GridView将获得中间的所有剩余空间。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:id="@+id/top_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button android:id="@+id/bottom_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
adamp的回答对我不起作用。 这是我发现完美的工作:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="@+id/top_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<Button android:id="@+id/bottom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<LinearLayout
android:id="@+id/container_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/bottom_button"
android:layout_below="@id/top_image"
android:gravity="center" >
<GridView android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
我已将其与其他LinearLayouts
用于代替GridView
,并在最内层布局上设置android:gravity="center"
,以使其所有文本视图和图像在水平和垂直方向上居中。
你可以试试这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:id="@+id/top_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView android:id="@+id/grid1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
将这些添加到您的网格视图:
android:layout_above="@id/bottom_button"
android:layout_below="@id/top_image"
使用这样的东西
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/table">
<TableRow
android:weightSum="100">
<ImageView android:id="@+id/image1"
android:src="@drawable/icon"
android:visibility="visible"
android:gravity="center"
android:layout_weight="50" />
</TableRow>
<TableRow
android:weightSum="100">
<GridView
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</GridView>
</TableRow>
<TableRow
android:weightSum="100">
<Button android:id="@+id/btn1"
android:visibility="visible"
android:gravity="center"
android:layout_weight="50" />
</TableRow>
</TableLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.