如何将一个视图的基线与另一个视图的顶部对齐

时间:2015-07-23 13:42:17

标签: android android-layout

我需要这样做,绿色图标的基线与此白卡顶部对齐

enter image description here

在我的情况下,我可以将绿色图标的顶部对齐到白卡的顶部。在这种情况下,绿色图标的layout_marginTop不允许将其移动到顶部。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.wearable.view.CardFrame
        android:id="@+id/card_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEXT"
            android:textColor="@color/black"/>
    </android.support.wearable.view.CardFrame>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu"
        android:layout_alignRight="@id/card_home"
        android:layout_alignTop="@id/card_home"
        android:layout_marginBottom="30dp"/>
</RelativeLayout>

enter image description here

2 个答案:

答案 0 :(得分:1)

你走在正确的轨道上。尝试这样,诀窍是用marginTop替换<ImageView>中的marginBottom并为其赋值为负值,例如:

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/card_home"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#FF00FFFF"
            android:layout_centerInParent="true">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:textColor="#ff000000"/>
        </FrameLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/abc_btn_check_to_on_mtrl_015"
            android:layout_alignRight="@id/card_home"
            android:layout_alignTop="@id/card_home"
            android:layout_marginTop="-15dp"/>
    </RelativeLayout>

请注意最后一行:android:layout_marginTop="-15dp"

这给了我这样的东西: enter image description here

答案 1 :(得分:1)

您可以尝试使用negativ margin-top

   <ImageView
       ...
       android:layout_marginTop="-30dp"
       .../>