2016-06-14 107 views
2

我想创建这样的布局如何按中心点对齐视图?

enter image description here

然后我发现这个

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

所以我创造了这个布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.myapplication.MainActivity"> 
    <View 
     android:id="@+id/square" 
     android:layout_width="100dp" 
     android:background="#ff0000" 
     android:layout_centerInParent="true" 
     android:layout_height="100dp"/> 
    <ImageView 
     android:id="@+id/circle" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignTop="@id/square" 
     android:layout_alignLeft="@id/square" 
     android:layout_marginRight="25dp" 
     android:layout_marginBottom="25dp" 
     android:src="@mipmap/circle" /> 
</RelativeLayout> 

但布局保证金没有任何效果

输出是一样到

<ImageView 
    android:id="@+id/circle" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_alignTop="@id/square" 
    android:layout_alignLeft="@id/square" 
    android:src="@mipmap/circle" /> 

如何对齐视图的中心点,以另一种观点的某一点?

+0

是在你的代码水平和垂直工作的中心?试试 –

+0

将圆圈放置在正方形内,然后向上移动并留下完整的半径 –

回答

0

一个选项是给正方形一个边缘顶部和边距等于圆的半径的左边。让这两个元素具有属性alignParentLeft=true

+0

你试过这个吗?它工作吗? – therealprashant

1

嗨,我做了一个例子,我的是它帮助你

请在与输出

<FrameLayout 
       android:id="@+id/FrameMove" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="8dp"> 

       <LinearLayout 
        android:id="@+id/linearFramelayout" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="5dp" 
        android:layout_marginTop="10dp" 
        android:background="@drawable/frame_drawable" 
        android:gravity="center" 
        android:orientation="vertical" 
        android:paddingBottom="-50dp"> 

        <TextView 
         android:id="@+id/txtMove" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textColor="@android:color/white" 
         android:textSize="20sp" 
         android:textStyle="bold"/> 
       </LinearLayout> 


       <ImageView 
        android:id="@+id/imgClose" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="end|top" 
        android:layout_marginRight="-8dp" 
        android:layout_marginTop="-8dp" 
        android:src="@drawable/ic_action_close_white"/> 

      </FrameLayout> 

Output

+0

这个答案很难适用于我目前的布局,但最后我从这个答案中得到了启发,我只需要设置android:layout_marginRight =“ - 25dp”,那么问题就解决了,所以这仍然是一个有用的答案 –

0

代码,如果你知道宽度和你的圆形图像的高度,那么这可能很容易 你需要使用一个框架布局,并给出一个圆的宽度和高度的一半的正方形的左和顶边距