1

У меня есть переключатель, ведьма выровнена слева от макета, Я хочу выровнять TextView справа от него И до середины его высоты , Я сделал два примера фотографии, чтобы объяснить это более четко.Как выровнять по правому и среднему по высоте в макете

Это то, что я есть (где А коммутатор, б является TextView):

enter image description here

Это то, что я хочу добиться:

enter image description here

Мои код компоновки для коммутатора:

<Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CIR" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:checked="true" 
     android:gravity="center" 
     android:id="@+id/newProject_switch" 
     android:layout_below="@+id/newProject_networkId" 
     android:layout_marginTop="10dp" 
     android:layout_alignParentLeft="true" 
     /> 

Мой код макет для TextView является:

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Best Effort" 
     android:textSize="15dp" 
     android:id="@+id/textView2" 
     android:layout_alignBottom="@+id/newProject_switch" 
     android:gravity="right|center_vertical" 
     android:layout_toRightOf="@+id/newProject_switch" 
     /> 
+0

ПУБЛИКАЦИЯ полный код для размещения. – Drv

+0

Вы можете использовать relativeLayout, а затем установить TextView слева от него и по центру по вертикали – user6547359

ответ

2

Используйте этот путь.

Удалить дополнительные Margin вы установили в своем Switch и используете android:gravity="center_vertical. и установите TextView на android:gravity="center_vertical|right" так, чтобы она была правой и вертикальной.

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Switch 
      android:id="@+id/newProject_switch" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:checked="true" 
      android:gravity="center_vertical" 
      android:text="CIR" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_alignBottom="@+id/newProject_switch" 
      android:layout_toRightOf="@+id/newProject_switch" 
      android:gravity="center_vertical|right" 
      android:text="Best Effort" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textSize="15dp" /> 
    </LinearLayout> 

enter image description here

+0

с указанием, android: layout_height, исправил его для меня. thnx! –

+0

@ user2560571 рад помочь вам ... – Ironman

0

Добавить эту строку в TextView

android:layout_centerVertical="true" 
0

меня удалить android:layout_marginTop="10dp" и добавить android:layout_gravity="center_vertical" к вашему Switch.
И изменить android:gravity="right|center_vertical" к android:layout_gravity="right|center_vertical" в вашем TextView

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#e4e4e4" 
     android:orientation="horizontal" 
     > 
     <Switch 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="CIR" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:checked="true" 
      android:gravity="center" 
      android:id="@+id/newProject_switch" 
      android:layout_below="@+id/newProject_networkId" 
      android:layout_alignParentLeft="true" 
      android:layout_gravity="center_vertical" 
      /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Best Effort" 
      android:textSize="15dp" 
      android:id="@+id/textView2" 
      android:layout_alignBottom="@+id/newProject_switch" 
      android:layout_gravity="right|center_vertical" 
      android:layout_toRightOf="@+id/newProject_switch" 
      /> 
    </LinearLayout> 

enter image description here

 Смежные вопросы

  • Нет связанных вопросов^_^