2017-09-10 1 views
1

스택 오버 플로우에 대한 "layout_gravity Bottom/Top in LinearLayout"과 관련하여 수백만 가지 질문과 대답이 있지만 여전히 문제를 해결하지 못했습니다.Android layout_gravity LinearLayout의 하단/상단

"맨 위를 맨 위로 올려주세요"라고 말한 EditText와 "맨 아래에 나를 넣어주세요"라고 쓰여있는 TextView를 배치하고 싶습니다. 내 꿈은 아주 기본이지만 나는 그것을 이룰 수 없다 !!

아무도 도와 줄 수 있습니까?

내 XML입니다 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    android:weightSum="1"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="210dp" 
     android:background="@android:color/holo_green_light" 
     android:gravity="center_vertical" 
     android:orientation="vertical" 
     android:weightSum="1"> 

     <EditText 
      android:id="@+id/editText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="textPersonName" 
      android:text="Please place me at very top" /> 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:background="@android:color/background_light" 
      android:text="TextView" 
      android:layout_weight="0.19" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="248dp" 
     android:layout_weight="0.70" 
     android:background="@color/colorAccent" 
     android:gravity="center_vertical" 
     android:orientation="vertical" 
     android:weightSum="1"> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="62dp" 
      android:layout_gravity="top" 
      android:text="Button" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.16" 
      android:background="@android:color/background_light" 
      android:text="Please place me at very bottom" /> 

    </LinearLayout> 

</LinearLayout> 

그리고 이것은 내 출력 :

enter image description here

+0

ConstraintLayout을 사용하지 않는 이유는 무엇입니까? –

+0

즉, 가중치를 사용할 때'android : layout_height'를 남겨 두는 것이 일반적으로 잘못되었습니다. 게다가 중심 중력을 적용했기 때문에 왜 위쪽이나 아래쪽에 있어야하는지 명확하지 않습니다. –

+1

이것은 LinearLayout으로 생성 된 화면이며 모든 화면을 변경하지 않으려 고합니다.버튼 하나만 추가하고 싶습니다. (이 버튼은 하단에 위치해야합니다.) – Grcn

답변

0

당신은을 배치 할 두 번째 선형 레이아웃 중력 바닥을 설정 한 하단의 조회수

변화 제의 LinearLayout에이 라인 :

android:gravity="center_vertical" 

다음과

android:gravity="top" 

변화 제의 LinearLayout에이 라인 :

android:gravity="center_vertical" 

사람 :

android:gravity="bottom" 

주 :이 성능이 저하됩니다 중첩 된 가중치를 사용하지 마십시오.

+0

상단 텍스트보기는 어떻습니까? –

+0

이제 내 코드가 업데이트되었습니다. –

0

LinearLayout가 작동하지 않는 경우, 강제로 사용하지 마십시오.

  1. 중첩 된 LinearLayouts 및 가중치는 성능에 좋지 않습니다.

  2. RelativeLayout의 (또는 ConstraintLayout)가 자연스럽게 ViewGroups

의 앵커 지점에서 물건을 배치하기위한 것입니다 당신이 다른 속성을 기반으로 중심으로 텍스트와 버튼을한다고 가정

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:weightSum="2"> 


    <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"> 

     <EditText 
       android:id="@+id/editText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:ems="10" 
       android:layout_alignParentTop="true" 
       android:inputType="textPersonName" 
       android:text="Please place me at very top" /> 

     <TextView 
       android:id="@+id/textView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:background="@android:color/background_light" 
       android:text="TextView" /> 

    </RelativeLayout> 

    <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@android:color/holo_red_light" 
      android:orientation="vertical"> 

     <Button 
       android:id="@+id/button" 
       android:layout_width="match_parent" 
       android:layout_height="62dp" 
       android:layout_centerInParent="true" 
       android:text="Button"/> 

     <TextView 
       android:id="@+id/textView2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:background="@android:color/background_light" 
       android:text="Please place me at very bottom"/> 

    </RelativeLayout> 

</LinearLayout> 

또는 두 번째 LinearLayout에 android:gravity="bottom" 만 있으면되고 그 안에있는 모든 뷰가 맨 아래에 있습니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:weightSum="1"> 

    <!-- Top Gravity --> 
    <LinearLayout 
      android:gravity="top" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:background="@android:color/holo_green_light" 
      android:orientation="vertical" 
      android:layout_weight="0.5"> 

     <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Please place me at very top"/> 

     <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/background_light" 
       android:text="TextView"/> 

    </LinearLayout> 

    <!-- Bottom Gravity --> 
    <LinearLayout 
      android:gravity="bottom" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.50" 
      android:background="@android:color/holo_red_light" 
      android:orientation="vertical"> 

     <Button 
       android:layout_width="match_parent" 
       android:layout_height="62dp" 
       android:text="Button"/> 

     <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/background_light" 
       android:text="Please place me at very bottom"/> 

    </LinearLayout> 

</LinearLayout> 
관련 문제