2016-06-14 112 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

代碼,如果你知道寬度和你的圓形圖像的高度,那麼這可能很容易 你需要使用一個框架佈局,並給出一個圓的寬度和高度的一半的正方形的左和頂邊距