如何在相对布局中居中两个视图?
这个任务很简单:有两个按钮和一个上面的TextView
。所有的小部件都应该在相对布局中居中。我唯一的想法是创建第三个部件View
并将其用作按钮的中心轴。有任何想法吗?冗余布局不是一个好的解决方案。如何在相对布局中居中两个视图?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_name" />
<View
android:id="@+id/view_axis"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_below="@id/tv_progress"
android:layout_centerInParent="true" />
<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_progress"
android:layout_toLeftOf="@id/view_axis"
android:text="@string/start" />
<Button
android:id="@+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_progress"
android:layout_toRightOf="@id/view_axis"
android:text="@string/stop" />
</RelativeLayout>
如果我知道你想正确的,你可以把Button
S IN一个LinearLayout
和中心是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/tv_progress">
<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
<Button
android:id="@+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop" />
</LinearLayout>
我不知道如果这是你通过一个意思“多余的布局”,但如果它给你你想要的东西,那么这样做很好。
当然,我可以。但它是一个多余的'LinearLayout'。阅读完[Android布局技巧#3](http://android-developers.blogspot.ru/2009/03/android-layout-tricks-3-optimize-by.html)和[Android布局技巧#1]后(http://android-developers.blogspot.ru/2009/02/android-layout-tricks-1.html),我开始更仔细地构建布局。 –
以这种方式使用它不会伤害你。也许如果它在'ListView'中,但如果不是这不是问题。你不需要太多不必要的嵌套'布局',但在这种情况下,它是我看到的最简单的方法。所以这不是没有必要的,只有一个嵌套'布局' – codeMagic
谢谢!我同意,在这种情况下,嵌套的'LinearLayout'是可以的。 –
这将垂直和水平居中+全块由TextView的按钮的
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
<Button
android:id="@+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/sp_rooms"
android:layout_toLeftOf="@id/space"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Space
android:id="@+id/space"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_registration"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/space"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
看看这里,这个答案,该答案打勾没有什么最初问。 http://*.com/questions/10904864/relativelayout-gravity-center-not-working/13280255#13280255 – user1806772