2016-10-05 111 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

相关问题