中心对齐视图相对于另一个视图

时间:2016-10-03 09:10:46

标签: android android-layout view android-xml

我想将view1居中对齐另一个view2,而view2和view1都是自定义大小。

View1
|------------------------|
|                        |
|                        |
|     |---------|        |
|     |   View2 |        |
|     |---------|        |
|                        |
|------------------------|

注意: View2位于view1的中间/中心,而view1不是父级,我该如何实现?

我的尝试:

<!-- some xml of another parents -->
<RelativeLayout
android:layout_width="match_parent"
    android:layout_height="match_parent"
>
<ImageView
        android:id="@+id/pin"
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:src="@drawable/image"
        android:layout_centerInParent="true"/>
</RelativeLayout>
<!--- long xml --->

1 个答案:

答案 0 :(得分:1)

尝试以下代码... itz为我工作

让View1 = ImageView; 视图2 = TextView的;

<?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" >


<ImageView
    android:id="@+id/overlapImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"

    android:src="@drawable/common_plus_signin_btn_text_dark_pressed" />
<TextView
    android:id="@+id/textViewTittle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Center Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="bold" />

</RelativeLayout>
相关问题