2016-10-05 116 views
0

linearLayout裏面我已經放了三個RelativeLayout s和weight = 1。我想RelativeLayout s在屏幕的中心。我如何管理他們的位置?下面在LinearLayout中居中多個RelativeLayout

是我xml代碼:

<LinearLayout 
      android:id="@+id/layout_circular_items" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toBottomOf="@+id/view_bg_profile_img" 
      android:padding="16dp" 
      android:layout_marginTop="8dp" 
      android:layout_marginEnd="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginStart="8dp" 
      android:layout_marginLeft="8dp" 
      android:background="@color/color_backgrounds_light"> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/circle_txt_view" 
       android:layout_margin="8dp" 
       android:gravity="center" 
       android:layout_gravity="center"> 

       <TextView 
        android:id="@+id/txt_view_scores" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="2843" 
        android:textSize="@dimen/dim_txt_size_times" 
        android:gravity="center" 
        android:foregroundGravity="center" 
        android:layout_centerHorizontal="true"/> 

       <TextView 
        android:id="@+id/txt_view_scores_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="scores" 
        android:layout_below="@id/txt_view_scores" 
        android:textSize="@dimen/dim_txt_size_times_title" 
        android:gravity="center" 
        android:foregroundGravity="center" 
        android:layout_centerHorizontal="true"/> 

      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/circle_txt_view" 
       android:layout_margin="8dp" 
       android:gravity="center" 
       android:layout_gravity="center"> 

       <TextView 
        android:id="@+id/txt_view_purchase" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="12" 
        android:textSize="@dimen/dim_txt_size_times" 
        android:gravity="center" 
        android:foregroundGravity="center" 
        android:layout_centerHorizontal="true"/> 

       <TextView 
        android:id="@+id/txt_view_purchase_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="purchase" 
        android:layout_below="@id/txt_view_purchase" 
        android:textSize="@dimen/dim_txt_size_times_title" 
        android:gravity="center" 
        android:foregroundGravity="center" 
        android:layout_centerHorizontal="true"/> 

      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/circle_txt_view" 
       android:layout_margin="8dp" 
       android:gravity="center" 
       android:layout_gravity="center"> 

       <TextView 
        android:id="@+id/txt_view_cash" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="21450" 
        android:textSize="@dimen/dim_txt_size_times" 
        android:gravity="center" 
        android:foregroundGravity="center" 
        android:layout_centerHorizontal="true"/> 

       <TextView 
        android:id="@+id/txt_view_cash_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="cash" 
        android:layout_below="@id/txt_view_cash" 
        android:textSize="@dimen/dim_txt_size_times_title" 
        android:gravity="center" 
        android:foregroundGravity="center" 
        android:layout_centerHorizontal="true"/> 

      </RelativeLayout> 

     </LinearLayout> 

,這是輸出的screen shot

enter image description here

我要紅色的空間是相同的。

+0

集_android:比重= 「中心」 機器人:layout_gravity = 「中心」 _爲_LinearLayout_ – Piyush

回答

1

在你上面的LinearLayout

只需使用android:gravity="center"

它肯定會工作。如果它不起作用,請從RelativeLayout中移除所有重力。只能在頂部的LinearLayout中使用android:gravity="center"

+0

它的工作!無需從'RelativeLayout'中移除'gravity'。只是與前兩行一起工作。 –

+1

是的,你是對的。實際上重力是LinearLayout的一個屬性,所以如果你在RelativeLayout中使用重力,它不會產生任何效果。 –

0

你可以試試這個代碼和編輯時請根據需要

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/layout_circular_items" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_centerInParent="true" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="8dp" 
     android:gravity="center"> 

     <TextView 
      android:id="@+id/txt_view_scores" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:foregroundGravity="center" 
      android:gravity="center" 
      android:text="2843" /> 

     <TextView 
      android:id="@+id/txt_view_scores_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/txt_view_scores" 
      android:layout_centerHorizontal="true" 
      android:foregroundGravity="center" 
      android:gravity="center" 
      android:text="scores" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="8dp" 
     android:gravity="center"> 

     <TextView 
      android:id="@+id/txt_view_purchase" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:foregroundGravity="center" 
      android:gravity="center" 
      android:text="12" /> 

     <TextView 
      android:id="@+id/txt_view_purchase_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/txt_view_purchase" 
      android:layout_centerHorizontal="true" 
      android:foregroundGravity="center" 
      android:gravity="center" 
      android:text="purchase" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center"> 

     <TextView 
      android:id="@+id/txt_view_cash" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:foregroundGravity="center" 
      android:gravity="center" 
      android:text="21450" /> 

     <TextView 
      android:id="@+id/txt_view_cash_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/txt_view_cash" 
      android:layout_centerHorizontal="true" 
      android:foregroundGravity="center" 
      android:gravity="center" 
      android:text="cash" /> 

    </RelativeLayout> 
</LinearLayout> 

0

雖然這個問題回答正確,但只是想增加更多的解釋。

當你想改變孩子的位置,你必須與他們的父母gravity一起工作,不需要改變孩子的重力。在你的情況,你必須簡單地改變你的LinearLayout的重力centercenter_horizontal這樣的:

<LinearLayout 
      android:id="@+id/layout_circular_items" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      <!-- Add this line--> 
      android:gravity="center" 
> ... 

android:gravity是觀的內部重力。這意味着,在 它的內容應該對齊哪個方向。

這個環節也很有用,以獲取更多關於gravitylayout_gravity

Gravity and layout_gravity on Android

相關問題