I have a RelativeLayout
with views aligned relative to it (their parent), using (e.g.):
我有一个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 (e.g. 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 (e.g.):
我已经尝试设置视图的边距(例如):
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!
2 个解决方案
#1
57
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!"/>
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!"/>
#2
0
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. Here's what I ended up doing.
我今晚刚开始进行Android编程,所以这可能是旧的或者是个坏主意......但是我正在修补上面的答案并发现将元素定位在我的其他元素下面导致了一个问题 - 填充是否会阻碍或者一些东西。这就是我最终做的事情。
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美分!
#1
57
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!"/>
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!"/>
#2
0
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. Here's what I ended up doing.
我今晚刚开始进行Android编程,所以这可能是旧的或者是个坏主意......但是我正在修补上面的答案并发现将元素定位在我的其他元素下面导致了一个问题 - 填充是否会阻碍或者一些东西。这就是我最终做的事情。
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美分!