简体   繁体   English

在相对布局中将视图的顶部与另一个视图的底部对齐

[英]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.

相关问题 RelativeLayout,对齐另一个视图底部的视图,但始终低于另一个视图 - RelativeLayout, align a view at the bottom of another view but always below another 相对布局顶部和底部对齐 - Relativelayout align top and bottom 如何在RelativeLayout中将视图定位在另一个视图的顶部? - How to position a view on top of another view in RelativeLayout? Android RelativeLayout 在另一个视图的右上角对齐一个视图的中心 - Android RelativeLayout align center of one view on top right corner of another view ConstraintLayout - 将视图对齐到另一个视图的顶部或底部,具体取决于另一个视图的可见性 - ConstraintLayout - Align view to top or bottom of another view, depending on the visibility of the other view 将相对布局中的中心视图与相对布局外的另一个视图对齐 - Align center view in relativealyout to another view outside of the relativelayout 如何在RelativeLayout中将视图与另一个视图对齐而不留空白? - How to align a view to another view without margin in RelativeLayout? 我如何将相对布局中的视图与右上角对齐 - How do i align a view in RelativeLayout to top-right corner 将视图动态添加到RelativeLayout的底部 - Dynamically add view to the bottom of RelativeLayout 在RelativeLayout中查看另一个视图 - Center view to another in RelativeLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM