简体   繁体   English

Android - 如何将视图定位在相对于其父级的中心/顶部/底部(等)的偏移量中?

[英]Android - How do I position views in an offset relative to the center/top/bottom (etc.) of their parent?

I have a RelativeLayout with views aligned relative to it (their parent), using (eg): 我有一个RelativeLayout ,其视图相对于它(他们的父),使用(例如):

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

and now I wonder, how can I position the View relative to the bottom of its parent, but at an offset (eg 100 pixels above the bottom, 20 pixels from the left, etc.), or similarly with the center (30 pixels under the center)? 现在我想知道,我如何相对于其父级的底部定位视图,但是在偏移处(例如,底部上方100像素,左侧20像素等),或类似于中心(30像素下)中心)?

必要布局的示意图

I've tried setting the margins of the View (eg): 我已经尝试设置视图的边距(例如):

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

before applying it to the textView , but that didn't work. 在将它应用于textView ,但这不起作用。

It seems like an extremely useful way to align views for various screen sizes. 这似乎是一种非常有用的方法来对齐各种屏幕尺寸的视图。

It's important for me to achieve this in code, without using xmls. 在不使用xmls的情况下,在代码中实现此功能非常重要。

Thanks! 谢谢!

you have 2 ways, the easies is using a anchor empty view positioned at the center 你有2种方法,简单就是使用位于中心的锚空视图

<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!"/>

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) 或者你可以将视图居中并使用pading por定位(你必须加倍使用的填充因为它溢出屏幕中心以下)

 <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!"/>

I just started Android programming tonight, so this may be old or a bad idea... but I was tinkering with the above answer and found positioning an element below my other element was causing a problem - either the padding would be in the way or something. 我今晚刚刚开始安装Android程序,所以这可能是旧的或者是个坏主意...但是我正在修补上面的答案并发现在我的另一个元素下方定位一个元素导致了一个问题 - 填充是否会阻碍或者一些东西。 Here's what I ended up doing. 这就是我最终做的事情。

Using the anchor technique, I also gave the anchor a height (double what you need), and then aligned my element with the top. 使用锚技术,我还给锚一个高度(你需要的两倍),然后将我的元素与顶部对齐。 That way it'd be in the middle of the view minus half the height of the anchor! 那样它就会在视图中间减去锚点高度的一半!

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

<TextView
    android:id="@+id/heading"
    android:text="@string/heading"
    android:textColor="@android:color/white"
    android:textSize="24sp"
    android:layout_alignTop="@id/anchor"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/subheading"
    android:text="@string/subheading"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    android:layout_below="@+id/heading"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

The other answer helped a lot but I just wanted to give my 2 cents! 另一个答案帮了很多,但我只是想给我2美分!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android和底视图之间居中查看视图 - Center a View in Android between bottom and top views 如何指定视频视图的位置? (左上,右上,左下…等) - How can i specify the position of a videoview? (top left, top right, bottom left… etc ) 如何从底部/中心定位按钮? - How Do I Position Button From Bottom/Center? 如何在Android中以特定坐标在彼此之上放置两个视图? - How do I position a two views, on top of each other, at certain coordinates, in Android? Android 控件的 Position(“主页”、“返回”等) - Position of Android controls ("home", "back", etc.) 如何在Android应用程序抽屉中添加按钮,开关等? - How do I add buttons, switches etc. to an android app drawer? 如何在Android的Soti MobiControl上阻止除蓝牙,WiFi等以外的所有设置 - How do I block all settings except BlueTooth,WiFi, etc. on Soti MobiControl for Android 如何在 Android 布局中居中多个视图? - How do I center several views in an Android layout? 如何在Android的相对布局中设置图片视图的位置? - How do I set the position of an image view in a relative layout in Android? 如何获得相对于Android中另一个视图的视图位置? - How do I get a view position relative to another view in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM