2012-04-21 2 views
1

다음 코드가 내 TextView를 화면의 왼쪽 상단에 배치하는 이유를 이해할 수 없습니다. layout_gravity에 대한 나의 이해를 바탕으로 내 TextView를 내 화면의 맨 아래에 배치해야합니다.안드로이드 layout_gravity

누군가 나를 위해이 부분을 비춰 줄 수 있습니까? 이 선형 레이아웃은 매우 단순 해 보였음에도 불구하고 나에게 수많은 골칫거리를 안겨주었습니다. 어떻게 그렇게 단순한 것처럼 보이는 것이 왕의 고통이 될 수 있습니까?

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:background="#FF0000"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" 
      android:layout_gravity="bottom" 
      android:background="#FFF" 
      />  

    </LinearLayout> 

답변

0
<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#FF0000"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" 
      android:layout_alignParentBottom="true" 
      android:background="#FFF" 
      />  

    </RelativeLayout> 

당신이 바닥을 정렬 할 수 있었다 상대적 레이아웃을 사용할 수 있습니다 쉽게

+0

팁 주셔서 감사합니다. 내 '실제'레이아웃의 대부분을 RelativeLayout으로 기울이고있었습니다. RelativeLayout으로 옮기기 전에 LinearLayout을 완전히 이해하고 싶었 기 때문에이 예제를 사용했습니다. 처음에는 그렇게 단순한 것처럼 보이는 것이 내 문서에 대한 설명에 대한 나의 이해를 바탕으로 완전히 직관적이지 않은 방법으로 내 마음을 불고있었습니다. 이보다 더 단순한 예를 생각할 수 없었지만, 텍스트를 원하는대로 배치하는 데 많은 어려움을 겪고있었습니다. –

+0

단순히 가로 또는 세로 레이아웃을 원한다면 선형을 사용하면 상대를 사용하는 것이 좋습니다. ofcourse re, ative 꽤 그렇지만 선형이지만 일단 이해하면 훨씬 더 쉽습니다. –

0

이 코드는 당신이 원하는 것을 할 생각 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:gravity="bottom" 
    android:background="#FF0000"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     android:background="#FFF" 
     />  

</LinearLayout> 

을 그리고 당신은 중앙에 텍스트 뷰를 원하는 경우, 당신은 사용할 수 있습니다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:gravity="bottom" 
    android:background="#FF0000"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     android:layout_gravity="center" 
     android:background="#FFF" 
     />  

</LinearLayout> 

희망이 당신을 돕습니다.

+0

선형 레이아웃에서 둘 이상의 자식 뷰가 있다면 그 중 하나가 모두 아래쪽에 정렬 될 것인가 아닌가? –

+0

@Agarwal, 네,하지만 시도해 보길 권합니다. 어떻게됩니까? ... – amp

0

레이아웃의 단순성이라면 LinearLayout에 포장해서는 안됩니다.

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:background="#FFF" 
    android:text="HELLO HELLO" /> 

당신이 비록 포장해야하는 경우에는 수직 방향 제거 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FF0000" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="#FFF" 
     android:text="HELLO HELLO" /> 

</LinearLayout> 
+0

왜 수직 방향을 제거하면 layout_gravity가 작동하는 방식으로 작동합니까? 수직 방향이 어떻게 든 layout_gravity가 작동하는 방식을 변경합니까? 확실히 그럴 것 같습니다. 그러나 나는이 행동의 논리를 이해할 수 없다. 나는 그것에 대한 좋은 이유가 있다고 확신한다. 나는 지금 그것을 보지 않고있다. –

+0

Ooops ...이 게시물 바로 아래에 설명되어 있습니다. 첫 코멘트를 올렸을 때 아직 그걸 만들지 않았습니까?:) –

+0

@JustinJenkins 왜냐하면 수직 선형 레이아웃은 어린이들을 '수직으로'쌓아 놓으려고 할 때 바닥 중력을 무시하기 때문에 어떤 수평 중력도 작용할 것입니다. – Blundell

2

있는 LinearLayout은 수직 정렬을 의미 모든 layout_gravity 매개 변수를 무시합니다 수직 방향으로 설정합니다. 예를 들어 "아래"는 작동하지 않지만 "가운데 수평"이 작동합니다. (수직 정렬은 레이아웃의 각 뷰가 레이아웃의 다른 뷰 아래에 배치되도록 항상 수행됩니다.)

상대 레이아웃은 갈 방법이 될 수도 있고 아마이 코드를 사용할 수도 있습니다 당신이 원하는대로.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FF0000" 
> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     android:gravity="bottom" 
     android:layout_weight="1.0" 
     android:background="#FFF" /> 

</LinearLayout> 

편집blog 더 일을 설명합니다.

+0

이 설명에 대해 너무 고마워요! 지금은 이해. 매우 감사! –

+0

@ Justin Jenkins 그때 도움이된다면 해결 된 것으로 표시해야합니다. 게시물에 오는 사람들이이 질문이 가장 적합한 답변이라는 것을 알게 될 것입니다 ... –

+0

하지만 그 사람은 내 대답을 훔쳤습니다. ( – Blundell