如何将一个视图的顶部与另一个视图的垂直中心对齐?

4
我需要将View2的左上角与View1的中心对齐,但我无法想出如何实现。

enter image description here


你尝试过以编程方式实现它,还是只用XML? - Ken Wolf
@KenWolf 没有,我知道如何在代码中实现,非常简单,只需获取View1的尺寸和坐标,然后相应地重新定位View2。我正在尝试在XML中实现它,除非没有其他选择。 - ilomambo
检查这些内容,它们可能会帮到你: http://stackoverflow.com/questions/3758932/layouts-on-top-of-each-other https://dev59.com/QHE85IYBdhLWcg3wwWQb http://stackoverflow.com/questions/7888337/how-do-i-put-buttons-on-top-of-each-other-in-same-xml-layout - Rachita Nanda
@ilomambo 看不出你如何在XML中完成它,而不绝对指定尺寸等...但让我们看看 :) - Ken Wolf
2
@KenWolf 你可能是对的。似乎安卓SDK团队中没有人认为layout_toCenterOf是一个必要的属性。 - ilomambo
显示剩余3条评论
3个回答

1
这是如何操作的!
创建一个像以下的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:background="#FFFF99" >

    <LinearLayout
        android:id="@+id/view1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:background="#440000"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/view1"
        android:layout_alignTop="@id/view1"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/view2"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:background="#99004400"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

在你的活动中。
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
         LinearLayout v1=(LinearLayout)findViewById(R.id.view1);
            LinearLayout v2=(LinearLayout)findViewById(R.id.view2);
            LinearLayout container=(LinearLayout)findViewById(R.id.container);
            int heightV1=v1.getHeight();
            int widthV1=v1.getWidth();
            container.setPadding(widthV1/2, heightV1/2, 0, 0);
    }

给出类似的输出

enter image description here


0

试一下这个

  RelativeLayout.LayoutParams gpsViewLayoutParams = new     RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
gpsViewLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT);
this.relativeLayout.addView(gpsView,gpsViewLayoutParams); 

1
你是否粘贴了正确的代码?我只看到一个视图,它与其父视图的右上角对齐。 - ilomambo

-3

这对你有帮助吗?

<RelativeLayout>
<View1 />
<View2
 Layout_marginTop= (view1Height/2)+view1margin
 Layout_marginLeft=(view1Width/2)+view1margin /> 
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接