2010-01-22 10 views
11

하나의 TextView와 하나의 ImageView로 구성된 간단한 LinearLayout을 가지고 있습니다. TextView의 텍스트가 오른쪽으로 정렬되기를 원하지만 결과는 텍스트가 왼쪽으로 정렬되었음을 나타냅니다. 레이아웃 XML에 문제가 있습니까? 감사.android : layout_gravity가 예상대로 작동하지 않습니다.


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:background="#E8E3E4"> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     > 
     <TextView android:layout_width="260dp" android:layout_height="wrap_content" 
      android:text="More Comments" android:textStyle="bold" android:background="#ff0000" 
      android:textColor="#121222" android:layout_gravity="right" /> 
     <ImageView android:layout_width="50dp" 
      android:layout_height="wrap_content" android:src="@drawable/arrow_right" 
      android:layout_gravity="center" android:scaleType="centerInside" /> 
    </LinearLayout> 
</LinearLayout> 
감사합니다, 마이클. 그러나 언급 한 해결 방법은 작동하지 않습니다.

나는 RelativeLayout의를 사용하려고하지만 여전히 날 같은 결과를 주었다 더 댓글 (아이콘) 내가 기대했던

는 RelativeLayout의의 XML은 다음과 같습니다 더 댓글 (아이콘)입니다. 거기에 문제가 있습니까? 마이클의 코멘트에서 해결 방법 다음

<?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"> 
    <TextView android:id="@+id/label" android:layout_width="260dp" 
     android:layout_height="wrap_content" android:text="More Comments" 
     android:textStyle="bold" android:background="#ff0000" 
     android:textColor="#121222" android:layout_gravity="right" /> 
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" android:layout_gravity="center" 
     android:scaleType="centerInside" android:layout_toRightOf="@id/label" /> 
</RelativeLayout> 

, 나는 아래의 XML이있다. 나는 출력이 될 것으로 예상 :

 RightLeft 

그러나 실제 출력은 다음과 같습니다

Right    Left 

그래서 해결 방법은 나를 위해 작동하지 않습니다. 이견있는 사람?

<LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right" 
        android:layout_weight="1.0"      
        android:text="RIGHT"/> 
      <TextView 
        android:layout_width="wrap_content" 

        android:layout_height="wrap_content" 
        android:layout_gravity="left" 
        android:text="LEFT"/> 
    </LinearLayout> 

+0

xml을 포함 할 수 있습니까? – jball

+0

그는 그렇게했고, 단지 형식을 지정하지 않았습니다. –

+0

(또한 : 수정되었으므로 코드가 *로 표시됩니다.) –

답변

7

나는 내 xml에 문제가있는 것을 알아 냈습니다. TextView의 텍스트를 오른쪽 정렬하려면 layout_gravity 대신 중력을 사용해야합니다. 다음 XML은 예상대로 작동합니다.

<?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"> 
    <TextView android:id="@+id/label" android:layout_width="260dp" 
     android:layout_height="wrap_content" android:text="More Comments" 
     android:textStyle="bold" android:background="#ff0000" 
     android:textColor="#121222" android:gravity="right" /> 
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" android:layout_gravity="center" 
     android:scaleType="centerInside" android:layout_toRightOf="@id/label" /> 
</RelativeLayout> 
+0

그러면이 대답을 '수락'으로 표시해야합니다 (질문 왼쪽의 눈금 표시를 클릭하십시오). –

1

나는 안드로이드와 단지 몇 가지 프로젝트를 수행했지만, 안드로이드 : layout_gravity 부모 내보기에 대한 정렬을 지정, 안드로이드 : 중력의 정렬을 지정합니다 중력을 지정하고있는 View 내의 뷰. 적절한 것을 지정했는지 확인하십시오. 문서에서는이 두 가지를 구분하지 않습니다.

샘플 XML을 게시하면 도움이 될 것입니다.

편집 :

XML을 게시했습니다. 적절한 속성을 설정 한 것처럼 보입니다. 그러나 텍스트를 오른쪽으로 정렬하는 경우 왜 선형 레이아웃에 배치해야할까요? 나는 왼쪽에서 오른쪽으로 이동하기 위해 그것을 변경하는 것이 좋습니다.

33

가로 방향이있는 Linear 레이아웃은 오른쪽/왼쪽 중력을 지원하지 않습니다. 해결 방법 here을 찾을 수 있습니다.

+0

+1, 나는이 대답을보기 전에이 정보를 찾으려고 약간의 시간을 보냈습니다. –

0

내 프로젝트에 대해 작성한 코드는 다음과 같습니다. 그것은 당신이 찾고있는 것을 성취합니다. 나는 컨트롤 타입을 바꿀 뿐이다. 당신은 벗어나야한다.

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:background="#ffdddddd"> 
    <TextView android:layout_width="260dp" 
     android:layout_height="wrap_content" 
     android:text="More Comments" 
     android:textStyle="bold" 
     android:background="#ff0000" 
     android:textColor="#121222" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentTop="true" /> 
    <ImageView android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" 
     android:scaleType="centerInside" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 
+0

안녕하세요 제러드, TextView로 ToogleButton을 대체하여 코드를 시도했습니다. 작동하지 않음 : TextView의 텍스트가 오른쪽으로 정렬되지 않았습니다. – user256239

+0

이 작업은 복사 만하면됩니다. – Jared

관련 문제