Android的 - 如何在定位的看法相对偏移到其父中心/顶/底部(等)? [英] Android - How do I position views in an offset relative to the center/top/bottom (etc.) of their parent?

查看:598
本文介绍了Android的 - 如何在定位的看法相对偏移到其父中心/顶/底部(等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RelativeLayout的,享有相对于它(其父),使用对齐(例如):

I have a RelativeLayout with views aligned relative to it (their parent), using (e.g.):

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(layoutParams);

和现在我不知道,我怎么能查看相对于其父的底部位置,但在一个偏移量(例如100像素底部上方,从左边等20像素),或类似的中心(中央下30像素)?

and now I wonder, how can I position the View relative to the bottom of its parent, but at an offset (e.g. 100 pixels above the bottom, 20 pixels from the left, etc.), or similarly with the center (30 pixels under the center)?

我试过设置视图的边缘(例如):

I've tried setting the margins of the View (e.g.):

layoutParams.setMargins(140, 0, 0, 0);

它应用到在的TextView ,但没有奏效。

这似乎是对齐的观点对各种屏幕尺寸的一个非常有效的方式。

It seems like an extremely useful way to align views for various screen sizes.

对我来说,做到这一点在code,而不使用XML的这一点很重要。

It's important for me to achieve this in code, without using xmls.

谢谢!

推荐答案

你有2个方面,easies使用位于市中心的锚空视图

you have 2 ways, the easies is using a anchor empty view positioned at the center

 <View
        android:id="@+id/anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />


<TextView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/anchor"
    android:layout_marginBottom="20dp"
    android:text="hello world!"/>

或者你也可以居中你的看法,并使用pading POR定位(因​​为它溢出屏幕下方的中央,你必须加倍使用的填充)

Or you can center your view and use pading por positioning (you must double the padding used because it overflows below the center of the screen)

  <TextView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingBottom="40dp"
        android:text="hello world!"/>

这篇关于Android的 - 如何在定位的看法相对偏移到其父中心/顶/底部(等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆