[英]Align the top of a View to the bottom of another view in a RelativeLayout
How can I align a View
's top to another View
's bottom?如何将
View
的顶部与另一个View
的底部对齐? I need two views one on top of another.我需要两个视图一个在另一个之上。 I achieved the right vertical position with a simple
android:layout_below="id_of_the_top_view"
, but I can't manage to align the View
s horizontally.我用一个简单的
android:layout_below="id_of_the_top_view"
获得了正确的垂直位置,但我无法水平对齐View
。 I would something like android:layout_alignTopToBottomOf="id_of_the_top_view"
, or in general something that lets me align a View
center (horizontal or vertical) to another View
center.我想要一些类似
android:layout_alignTopToBottomOf="id_of_the_top_view"
东西,或者一般来说让我将一个View
中心(水平或垂直)对齐到另一个View
中心的东西。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- center picker @ minutes -->
<NumberPicker
android:id="@+id/npicker_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<!-- left picker @ hours -->
<NumberPicker
android:id="@+id/npicker_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/npicker_minutes" />
<!-- right picker @ seconds -->
<NumberPicker
android:id="@+id/npicker_seconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/npicker_minutes" />
<TextView
android:id="@+id/tv_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/npicker_minutes"
android:layout_marginTop="20dp"
android:paddingBottom="15dp"
android:text="@string/minutes_short" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/npicker_hours"
android:layout_toLeftOf="@id/tv_minutes"
android:layout_marginTop="20dp"
android:paddingBottom="15dp"
android:text="@string/hours_short" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/npicker_seconds"
android:layout_toRightOf="@id/tv_minutes"
android:layout_marginTop="20dp"
android:paddingBottom="15dp"
android:text="@string/seconds_short" />
</RelativeLayout>
What I've got is this:我有的是这个:
And what I need to achieve is that the three TextView
s ("hh", "mm" and "ss"), are placed each one below one of the number pickers, with the horizontal center of the TextView
aligned to the horizontal center of the NumberPicker
.我需要实现的是将三个
TextView
s(“hh”、“mm”和“ss”)分别放置在其中一个数字选择器的下方,并且TextView
的水平中心与NumberPicker
。
You need to add this lines to your TextViews:您需要将此行添加到您的 TextViews:
android:layout_alignLeft="@id/npicker_minutes"
android:layout_alignRight="@id/npicker_minutes"
android:gravity="center_horizontal"
Complete sample of your code:完整的代码示例:
<!-- center picker @ minutes -->
<NumberPicker
android:id="@+id/npicker_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<!-- left picker @ hours -->
<NumberPicker
android:id="@+id/npicker_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/npicker_minutes" />
<!-- right picker @ seconds -->
<NumberPicker
android:id="@+id/npicker_seconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/npicker_minutes" />
<TextView
android:id="@+id/tv_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/npicker_minutes"
android:layout_alignRight="@id/npicker_minutes"
android:gravity="center_horizontal"
android:layout_below="@id/npicker_minutes"
android:layout_marginTop="20dp"
android:paddingBottom="15dp"
android:text="@string/minutes_short" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/npicker_hours"
android:layout_alignRight="@+id/npicker_hours"
android:gravity="center_horizontal"
android:layout_below="@id/npicker_hours"
android:layout_marginTop="20dp"
android:paddingBottom="15dp"
android:text="@string/hours_short" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/npicker_seconds"
android:layout_alignRight="@id/npicker_seconds"
android:gravity="center_horizontal"
android:layout_below="@id/npicker_seconds"
android:layout_marginTop="20dp"
android:paddingBottom="15dp"
android:text="@string/seconds_short" />
The alternative way is to use tree LinearLayouts, every with one NumberPicker and one TextView inside.另一种方法是使用树 LinearLayouts,每个都有一个 NumberPicker 和一个 TextView。
You can use the following properties to align the first and the third text view:您可以使用以下属性来对齐第一个和第三个文本视图:
android:layout_alignLeft="@+id/your_first_number_picker" //your first text view
android:layout_alignRight="@+id/your_third_number_picker" //your third text view
in my case i use android:layout_above就我而言,我使用 android:layout_above
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_bottom"
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.