一种观点的另一种视野的右上角的Andr​​oid RelativeLayout的居中 [英] Android RelativeLayout align center of one view on top right corner of another view

查看:121
本文介绍了一种观点的另一种视野的右上角的Andr​​oid RelativeLayout的居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有RelativeLayout的经验,但我从来没有穿过的方式来解决我psented用$ P $的问题上运行(除了硬编码边距值,这是我想避免的。)

I have experience with RelativeLayout but I've never run across a way to solve the problem I am presented with (aside from hard coding margin values, which I want to avoid.)

我想尝试创造一些像一个RelativeLayout的如下图:

I want to try to create something like the following image in a RelativeLayout:

这个盒子是自己的看法,我想获得一个包含橙色圆圈上包含蓝色中的视图的右上角为中心的视图。

The box is its own View and I want to get the View that contains the orange circle to be centered on the top right corner of the View that contains the blue box.

我试着用安卓alignTop =BoxView中的安卓alignRight =BoxView中的但放我的橙色圆圈完全在我的盒子。我希望它是这样的圆上方的框右上角为中心的。

I tried with android:alignTop="boxView" and android:alignRight="boxView" but that put my orange circle completely within my box. I want it to be so that the circle is centered above the top right corner of the box.

任何人都知道我怎样才能得到一个RelativeLayout的这种结果? preferably没有从屏幕上的橙色点视图的边缘有硬code的利润了。

Anybody know how I can get that outcome with a RelativeLayout? preferably without having to hardcode margins away from the edge of the screen for the orange dot view.

推荐答案

这code创建你在找什么,但确实使用空间。现在,您可以设置code保证金,如果这是您要创建一个动态结构。正如你可以看到我用切缘阴性移动右上方形状的蓝色框外。这些都需要你正在试图将圆的一半高度。你可以做到这一切在code到中心右上角圆圈。

This code creates what you are looking for but does use margins. Now you can set the margin in code if this is a dynamic structure you are creating. As you can see I used negative margins to move the upper right shape outside of the blue box. These need to be half the height of the circle you are trying to move. You can do all of this in code to center the circle in the upper right corner.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="#0000FF"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:layout_alignRight="@+id/linearLayout1"
            android:layout_alignTop="@+id/linearLayout1"
            android:layout_marginRight="-13dp"
            android:layout_marginTop="-13dp"
            android:background="#FF00FF"
            android:orientation="vertical" >
        </LinearLayout>

    </RelativeLayout>

这篇关于一种观点的另一种视野的右上角的Andr​​oid RelativeLayout的居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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