2016-11-30 3 views
1

안녕하세요, 저는 LinearLayout과 같은 부모 레이아웃을 가지고 있으며, 화면 중앙에 이미지 뷰를 배치해야합니다. 화면 하단에는 아이콘이 있습니다.안드로이드의 화면 중앙에 imageview와 두 개의 텍스트를 정렬하는 방법은 무엇입니까?

이미지 뷰를 제외한 모든 요소를 ​​배치했으며 두 개의 텍스트 뷰가 화면 가운데에 정렬되지 않았습니다. 다음은 레이아웃입니다.

<?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

의 LinearLayout 부모이 붙여 넣기 점점? 쇼 화면 – 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 문제입니다을보십시오. 루트의 LinearLayout에 무게 = "센터"와 마지막 RelativeLayout의를 제거하고

0

그냥 안드로이드를 추가하려면 부모 android:gravity="center"를 추가 당신은 상대적 레이아웃을 사용해야하고 그것은 작동 할 것입니다.

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> 

문제를 참조가 정렬됩니다 있도록 선형 레이아웃을 사용하고 있다고한다 : 그리고 LinearLayout

0

는 'layout_gravity'와 자식의 LinearLayout에서 '중력'매개 변수를 절단하는 방법과 출력 통해 UR의

<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.... 

> 
관련 문제