[英]How to center two views within a relative layout?
The task is simple: there are two buttons and a TextView
above them. 任务很简单:上面有两个按钮和一个
TextView
。 All the widgets shoud be centered within the relative layout. 所有小部件都应该在相对布局中居中。 The only one idea I have is create the third widget
View
and use it as a center axis for the buttons. 我唯一的想法是创建第三个小部件
View
并将其用作按钮的中心轴。 Any ideas? 有任何想法吗? A redundant layout isn't a good solution.
冗余布局不是一个好的解决方案。
<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>
If I understand what you want correctly, you can put the Button
s in a LinearLayout
and center that 如果我理解你想要什么,你可以将
Button
放在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>
I'm not sure if that's what you meant by a "redundant layout" but doing this is fine if it gives you what you want. 我不确定这是否是“冗余布局”的意思,但如果它能满足您的需求,那么这样做很好。
<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>
This will vertically and horizontally center the whole block consisting of the textview + buttons 这将垂直和水平居中整个块,包括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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.