1

Я хочу, чтобы моя карточка, чтобы выглядеть следующим образомВыравнивание элементов в cardview

enter image description here

Я держал мою раскладку как этот

<android.support.v7.widget.CardView 
     android:layout_gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     card_view:cardCornerRadius="2dp" 
     > 

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

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Order# GAMH2103" 
      android:layout_marginStart="5dp" 
      android:layout_marginLeft="5dp" 
      android:gravity="start" 
      android:textSize="15dp"/> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Confirmed" 
       android:drawableRight="@drawable/check" 
       android:textColor="#00FF00" 
       android:gravity="end" 
       android:textSize="15dp"/> 

     </LinearLayout> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@android:color/darker_gray" 
       android:layout_marginTop="5dp" 
       /> 

     </LinearLayout> 

Как-то, я не в состоянии получить отображается «подтвержденный» текст. Однако я вижу порядок #.

Я играл с gravity и layout_gravity, но так или иначе не смог пройти.

Пожалуйста, помогите.

Спасибо, Лакшман.

+1

Поскольку вы установили ширину «порядок # хотя» TextView 'android: layout_width =« match_parent »', поэтому «подтвержденный» TextView не может быть видимым. Измените на 'android: layout_width =" wrap_content "'. Вы можете добавить 'android: layout_weight =" 1 "', если вы хотите, чтобы «подтвержденный» TextView был всегда видимым. –

ответ

4

Используйте следующий макет. Это будет работать на вас.

Всегда рекомендуется использовать относительную компоновку вместо линейной компоновки для лучшего выравнивания.

<android.support.v7.widget.CardView 
    android:layout_gravity="center" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="2dp"> 

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

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Order# GAMH2103" 
      android:layout_marginStart="5dp" 
      android:layout_marginLeft="5dp" 
      android:gravity="start" 
      android:textSize="15dp"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Confirmed" 
      android:textColor="#00FF00" 
      android:gravity="end" 
      android:textSize="15dp"/> 


    </RelativeLayout> 

    </android.support.v7.widget.CardView> 
0

Простая относительная компоновка должна выполнять эту работу.

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Order# GAMH2103" 
     android:layout_marginStart="5dp" 
     android:layout_marginLeft="5dp" 
     android:gravity="start" 
     android:textSize="15dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Confirmed" 
     android:drawableRight="@drawable/check" 
     android:textColor="#00FF00" 
     android:layout_gravity="end" 
     android:textSize="15dp"/> 

</RelativeLayout> 

Обратите внимание на разнице между gravity и layout_gravity, если вы используете gravity в TextView, он относится к позиции текста внутри невидимого поле, который является границей TextView, так, например, layout_gravity на другая сторона относится к положению представления внутри родителя.

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