2013-09-11 6 views
0

layout.xmlКак центрировать выровнять 2 вида?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <RelativeLayout 
     android:id="@+id/viewPost" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:layout_marginTop="14dp" 
     android:background="@color/bg_blue" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/ic_viewPost" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_view" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_toRightOf="@id/ic_viewPost" 
      android:gravity="center" 
      android:text="View post" 
      android:textColor="@color/white" 
      android:textSize="14sp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/socialShare" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:layout_marginTop="14dp" 
     android:background="@color/bg_light_grey" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/ic_socialShare" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_share_post" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_toRightOf="@id/ic_socialShare" 
      android:gravity="center" 
      android:text="Share with others" 
      android:textColor="@color/text_dark_grey" 
      android:textSize="14sp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 
</LinearLayout> 

В графической компоновки:

enter image description here

На реальном устройстве:

enter image description here

В графическом макете он выглядит так, как я хотел, но на самом устройстве его совсем другое.

В 1-й относительной компоновке ImageView и TextView не выровнены.

Во 2-й относительной компоновке это выглядит так, как TextView снизу выравнивается с ImageView.

Thank you.

ответ

0

просто использовать это вместо пары ImageView-TextView

<TextView 
    android:drawableLeft="@drawable/ic_viewPost" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:text="View post" 
    android:textColor="@color/white" 
    android:textSize="14sp" 
    android:textStyle="bold" /> 
0

Remove андроид: гравитация = «центр» с обоего TextViews и check.it должен решить эту проблему, как вы уже используете центр тяжести в линейной компоновке

0

Добавьте этот атрибут в TextView и ImageView.

android:layout_centerVertical="true" 
Смежные вопросы