2016-11-30 70 views
1

您好我有父佈局作爲LinearLayout,我需要放置一個imageview在中心和兩個textview一個接一個在屏幕中心。屏幕底部是一個圖標。如何對齊imageview和屏幕中心的兩個文本android?

我放置了一切,但imageview和兩個textview不在屏幕中心對齊。這是佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@drawable/bg" 
    android:orientation="vertical"> 

    //this image and two textview, i need to place at center of screen 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_gravity="center" 
    android:orientation="vertical"> 
    <ImageView 
     android:id="@+id/seat_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/on_logo" 
     android:layout_marginBottom="@dimen/dp_size20" 
     /> 

    <TextView 
     android:id="@+id/seat_number" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Test1" 
     android:textAllCaps="false" 
     android:textColor="@color/white" 
     android:textSize="16dp" 
     android:gravity="center"/> 
    <TextView 
     android:id="@+id/another_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/dp_size10" 
     android:paddingBottom="@dimen/dp_size10" 
     android:text="TEst 2" 
     android:textAllCaps="false" 
     android:textColor="@color/white" 
     android:textSize="16dp" 
     android:gravity="center"/> 

</LinearLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/dp_size10"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="1" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentStart="true"> 

      <ImageView 
       android:id="@+id/on_boarding_circle_arrow" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_centerInParent="true" 
       android:background="@drawable/circle_arrow" /> 
</RelativeLayout> 
    </RelativeLayout> 
    </LinearLayout> 

我真的搞不清楚我在這個設計中做了什麼錯誤。 ImageView和兩個文本放置在屏幕頂部而不是屏幕中央。

+0

如何的輸出烏爾得到些什麼?顯示屏幕 – Gattsu

回答

2

我已經改變了一些尺寸和圖像,糾正這些,你很好去。

試試這個..

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_centerInParent="true" 
      android:orientation="vertical"> 
      <ImageView 
       android:id="@+id/seat_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@mipmap/ic_launcher" 
       android:layout_marginBottom="20dp" /> 
      <TextView 
       android:id="@+id/seat_number" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Test1" 
       android:textAllCaps="false" 
       android:textSize="16dp" 
       android:gravity="center"/> 
      <TextView 
       android:id="@+id/another_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="@dimen/dp_size10" 
       android:paddingBottom="@dimen/dp_size10" 
       android:text="TEst 2" 
       android:textAllCaps="false" 
       android:textColor="@color/white" 
       android:textSize="16dp" 
       android:gravity="center"/> 

     </LinearLayout> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="@dimen/dp_size10"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:weightSum="1" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentStart="true"> 

       <ImageView 
        android:id="@+id/on_boarding_circle_arrow" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:layout_centerInParent="true" 
        android:background="@mipmap/ic_launcher" /> 
      </RelativeLayout> 
     </RelativeLayout> 
    </RelativeLayout> 
</LinearLayout> 
0

試試這個代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@drawable/bg" 
    android:orientation="vertical"> 

    //this image and two textview, i need to place at center of screen 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:layout_gravity="center" 
    android:orientation="vertical"> 
    <ImageView 
     android:id="@+id/seat_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/on_logo" 
     android:layout_marginBottom="@dimen/dp_size20" 
     /> 

    <TextView 
     android:id="@+id/seat_number" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Test1" 
     android:textAllCaps="false" 
     android:textColor="@color/white" 
     android:textSize="16dp" 
     android:gravity="center"/> 
    <TextView 
     android:id="@+id/another_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/dp_size10" 
     android:paddingBottom="@dimen/dp_size10" 
     android:text="TEst 2" 
     android:textAllCaps="false" 
     android:textColor="@color/white" 
     android:textSize="16dp" 
     android:gravity="center"/> 

</LinearLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/dp_size10"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="1" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentStart="true"> 

      <ImageView 
       android:id="@+id/on_boarding_circle_arrow" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_centerInParent="true" 
       android:background="@drawable/circle_arrow" /> 
</RelativeLayout> 
    </RelativeLayout> 
    </LinearLayout> 
0

RelativeLayout底部的問題。並添加android:gravity="center"到父LinearLayout

0

只需添加機器人:在你的根的LinearLayout比重=「中心」,並刪除最後的RelativeLayout,看看

1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_centerInParent="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_gravity="center_vertical" 
     android:orientation="vertical"> 
     <ImageView 
      android:id="@+id/seat_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="20dp" 
      /> 

     <TextView 
      android:id="@+id/seat_number" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Test1" 
      android:textAllCaps="false" 
      android:textColor="@color/Grey" 
      android:textSize="16dp" 
      android:gravity="center"/> 
     <TextView 
      android:id="@+id/another_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="10dp" 
      android:paddingBottom="10dp" 
      android:text="TEst 2" 
      android:textAllCaps="false" 

      android:textSize="16dp" 
      android:gravity="center"/> 

    </LinearLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     > 

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

      <ImageView 
       android:id="@+id/on_boarding_circle_arrow" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_centerInParent="true" 
       android:background="@drawable/acircle" /> 
     </RelativeLayout> 
    </RelativeLayout> 
</RelativeLayout> 

的問題是,您使用的線性佈局,因此正在調整佈局。所以你需要使用相對佈局,它會工作

0

切 'layout_gravity',並從孩子的LinearLayout '重力' 參數並粘貼到家長的LinearLayout

<LinearLayout 

    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:layout_gravity="center" 
    android:orientation="vertical"> 

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
      <ImageView 
       android:id="@+id/seat_icon" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
     /> 
Your code.... 

>