2017-01-12 8 views
0

Я хотел бы спросить, есть ли способ центрировать LinearLayout на его разделителе, а не в содержимом?Центр LinearLayout на разделителе

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

Вот макет:

<LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:orientation="horizontal"> 

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

       <TextView 
        android:id="@+id/savings_accumulated_percentage_value_label" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Zrealizowano:" 

        android:layout_gravity="end" 
        /> 


       <TextView 
        android:id="@+id/savings_accumulated_value_label" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Zgromadzono:" 

        android:layout_gravity="end" 

        /> 

       <TextView 
        android:id="@+id/savings_target_value_label" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Cel:" 

        android:layout_gravity="end" 

        /> 

       <TextView 
        android:id="@+id/savings_target_date_label" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Data zakończenia:" 

        android:layout_gravity="end" 
        /> 

      </LinearLayout> 

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


       <TextView 
        android:id="@+id/savings_accumulated_percentage_value" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="18%" 
        android:textColor="@color/colorAccent" /> 

       <TextView 
        android:id="@+id/savings_accumulated_value" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="865 zł" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/savings_target_value" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="2 865 zł" 
        android:textColor="@color/colorPrimary" 
        android:textStyle="bold" 

        /> 

       <TextView 
        android:id="@+id/savings_target_date" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="03.07.2018" 
        /> 

      </LinearLayout> 


     </LinearLayout> 

enter image description here

+0

пожалуйста, поделитесь макет XML –

+0

ой, забыл сделать это, извините – AndroidBegginer

ответ

1

Вы можете использовать android:weightSum свойство родительского макета и android:weight собственности на дочерних компоновок для достижения этой цели ,

Общее правило: weightSum = сумма всех весов всех непосредственных детей (в приведенном ниже примере его 2 = 1 + 1). Хотя вы не получите никаких ошибок, если веса не равны, но результат может быть не таким, как вы ожидали.

Вот простой пример.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_centerInParent="true" 
    android:weightSum="2"> <!-- weightSum property --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> <!-- weight property --> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="hello"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> <!-- weight property --> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="hi"/> 
    </LinearLayout> 
</LinearLayout> 
+0

спасибо, работает отлично – AndroidBegginer

+0

@AndroidBegginer ура! –

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

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