【问题标题】:Android - How do I position views in an offset relative to the center/top/bottom (etc.) of their parent?Android - 如何将视图定位在相对于其父级的中心/顶部/底部(等)的偏移量中?
【发布时间】:2011-10-25 10:20:52
【问题描述】:

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

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

现在我想知道,我怎样才能将 View 相对于其父级的底部定位,但在偏移量(例如,底部上方 100 像素,距左侧 20 像素等),或者与中心类似(中心下方 30 像素)?

我尝试设置视图的边距(例如):

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

在将其应用到textView 之前,但这不起作用。

这似乎是一种非常有用的方法来对齐各种屏幕尺寸的视图。

不使用 xmls 在代码中实现这一点对我来说很重要。

谢谢!

【问题讨论】:

  • 从 xml 创建布局这是为了创建..
  • @PolamReddy 对不起,我不明白。你能改写一下吗?
  • 你动态创建有点难,从xml文件创建布局会很容易创建。
  • 您能否提示我如何在 xml 布局中执行此操作,我会尝试找出转换为动态版本的方法?谢谢。
  • 您可以在LinearLayout中使用android:paddingBottomandroid:marginTop

标签: android android-layout


【解决方案1】:

你有两种方法,最简单的方法是使用位于中心的锚空视图

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

或者您可以将视图居中并使用填充或定位(您必须将使用的填充加倍,因为它会在屏幕中心下方溢出)

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

【讨论】:

  • 多么美好的复活 :)。你会说优点是:anchor 方法更容易使用,但可能会浪费更多的 cpu/内存资源(使相对布局树更大)?另外,我最初的问题需要在代码中完成。除了使用类似:layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0) 之外,还有什么其他的吗?谢谢!
  • 对于一个包含几个元素的简单视图,这非常有效!
  • 我独立提出了完全相同的解决方案,甚至将视图称为“锚点”。唯一的区别是我将锚点的高度设置为“match_parent”而不是零。使用零有什么好处吗?
【解决方案2】:

我今晚刚开始进行 Android 编程,所以这可能是旧的或一个坏主意......但我正在修改上面的答案,发现将一个元素定位在我的另一个元素下方会导致问题 - 要么填充将在方式什么的。这就是我最终要做的。

使用锚点技术,我还给了锚点一个高度(你需要的两倍),然后将我的元素与顶部对齐。这样它就会在视图的中间减去锚点高度的一半!

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

另一个答案很有帮助,但我只想给我的 2 美分!

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多