2014-02-20 53 views
2

我试图对齐三个元素,第一个位于左边缘(Imageview),中间的文字视图为标题,右边缘为Imageview。使用RelativeLayout对齐三个元素

这是代码

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

    android:background="@color/fondo_main" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/RelativeLayout01" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" > 


     <ImageView 
      android:id="@+id/buttonback" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:src="@drawable/back" /> 
     <TextView 
      android:id="@+id/tittle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Tittle" 
     android:textColor="@color/azul_asde" 
      android:src="@drawable/upload" /> 
     <ImageView 
      android:id="@+id/buttonupload" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/upload" /> 
    </RelativeLayout> 
</LinearLayout> 

我怎么能在中心对齐丝毫不差?

+0

你可能想看看我的更新答案。 :) – SMR

回答

6

把这里面的TextView

android:layout_centerHorizontal="true" 
2

可以对齐它们使用toRightOftoLeftOf性能。试试这个

<TextView 
    android:id="@+id/tittle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:toRightOf="@id/buttonback" 
    android:toLeftOf="@+id/buttonupload" 
    android:singleLine="false" 
    android:maxLines="**YOUR CHOCICE**" 
    android:text="Tittle" 
    android:textColor="@color/azul_asde" 
    android:src="@drawable/upload" /> 

[编辑]同时使用toRightOftoLeftOf保证了即使在EditText文本太长然后在右侧buttonupload不会在屏幕外转移。

这是你不能使用android:layout_centerHorizontal="true" *

[注]你可能也想考虑android:singleLine="false"android:maxLines性能实现。

希望它有帮助。

3

试试这个

<LinearLayout 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" 
    android:background="@color/fondo_main" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/RelativeLayout01" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/buttonback" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:src="@drawable/back" /> 

     <TextView 
      android:id="@+id/tittle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/buttonback" 
      android:layout_alignRight="@+id/buttonupload" 
      android:gravity="center" 
      android:src="@drawable/upload" 
      android:text="Tittle" 
      android:textColor="@color/azul_asde" /> 

     <ImageView 
      android:id="@+id/buttonupload" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/upload" /> 
    </RelativeLayout> 

</LinearLayout> 
+0

这也适用于我。谢谢 – Hanzo

1

尝试这个

<LinearLayout 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" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/RelativeLayout01" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/buttonback" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_toLeftOf="@id/tittle" 
      android:src="@drawable/ic_launcher" /> 

     <TextView 
      android:id="@+id/tittle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:gravity="center" 
      android:src="@drawable/ic_launcher" 
      android:text="Tittle" 
      android:textColor="#000000" /> 

     <ImageView 
      android:id="@+id/buttonupload" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_toRightOf="@id/tittle" 
      android:src="@drawable/ic_launcher" /> 
    </RelativeLayout> 

</LinearLayout>