2016-06-03 3 views
2

제목은 약간 도발적이지만 일부 행동은 나를 설명 할 수없는 것으로 관찰됩니다.Android의 RelativeLayout을 올바르게 작동시키는 방법은 무엇입니까?

나는 각각 TextView tvX와 Spinner spX가 하나씩있는 세 세트의보기가 있습니다. X = A, B, C. textview는 회 전자에 대한 제목 또는 프롬프트이므로 각 TextView의 Spinner layout_toRight를 원합니다. 서로 참조하기 때문에 Spinner를 TextView에 layout_alignBaseline합니다. 이것은 올바르게 작동합니다. 이제 TextViews가 오른쪽 가장자리에 정렬되도록 세 개의 스택을 쌓아두기를 원합니다 (각각 후행 콜론이 있으며 정렬되어야합니다). Spinner는 왼쪽 가장자리를 정렬했습니다. 그래서 스택의

+----------+----------+ 
    |  tvA:|spA  | 
    +----------+----------+ 
    |  tvB:|spB  | 
    +----------+----------+ 
    |  tvC:|spC  | 
    +----------+----------+ 

B와 C보기는 각각 A와 B를 참조하는 속성을 얻습니다. layout_below 및 layout_alignLeft/Right입니다.

이제 tvB 및 tvC에는 tvA보다 긴 텍스트가 있습니다. 나는 RelativeLayout 들여 쓰기 tvA를 추측 했으므로 tvB와 tvC의 더 긴 텍스트를위한 공간이 있고 오른쪽 정렬됩니다. 대신 왼쪽 열 전체가 tvA의 텍스트 너비를 가져오고 tvB와 tvC가 각각 두 줄로 분리됩니다. 나는 tvB를 앵커로 만들고, tvA를 layout_above로 만들기 위해 임시로 재 배열하려고했다. tvA가 완전히 사라집니다. 어쨌든 적절한 해결책이되지 않을 것입니다. 다른 언어에서 텍스트의 상대적 길이가 다른 방향 일 수 있습니다! tvA에 문자를 추가하여 가장 길게 만들면 tvB 및 tvC는 더 이상 손상되지 않지만 콜론은 여전히 ​​정렬되지 않습니다! 실제로 tvB가 tvA 아래에 중앙에오고 tvC가 tvC 아래에 중앙에있는 것처럼 보입니다.

tvX를 왼쪽 정렬하고 spX를 오른쪽 정렬하면 가장 잘 보입니다. Spinner는 layout_width = "wrap_content"속성을 더 이상 따르지 않고 TextViews가 남긴 모든 공간을 채우도록 확장합니다. 중간의 불규칙한 정렬은 여전히 ​​나빠 보이기 때문에 정렬이 중간에 일어나기를 바랍니다.

Android Studio는 XML을 편집하는 동안 결과 레이아웃을 표시하고 커서가보기의 XML 정의에 있으면보기 가장자리도 표시합니다. 속성의 레이아웃 힌트가 무시되는 방식을 잘 볼 수 있습니다! 실제 전화에서 같은 일이 발생합니다. 여기

는 XML 코드 :

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="@dimen/indented_margin" 
     android:layout_marginBottom="5dp"> 

     <TextView 
      android:id="@+id/device_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:text="@string/settings_sb_device" /> 

     <Spinner 
      android:id="@+id/device" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:spinnerMode="dropdown" 
      android:layout_toRightOf="@id/device_title" 
      android:layout_alignBaseline="@id/device_title" /> 

     <TextView 
      android:id="@+id/can_speed_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:layout_below="@id/device_title" 
      android:layout_alignRight="@id/device_title" 
      android:text="@string/settings_sb_speed" /> 

     <Spinner 
      android:id="@+id/can_speed" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:spinnerMode="dropdown" 
      android:layout_toRightOf="@id/can_speed_title" 
      android:layout_alignBaseline="@id/can_speed_title" 
      android:layout_alignLeft="@id/device" 
      android:entries="@array/can_speeds" /> 

     <TextView 
      android:id="@+id/baudrate_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:layout_below="@id/can_speed_title" 
      android:layout_alignRight="@id/can_speed_title" 
      android:text="@string/settings_sb_baudrate" /> 

     <Spinner 
      android:id="@+id/baudrate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:spinnerMode="dropdown" 
      android:layout_toRightOf="@id/baudrate_title" 
      android:layout_alignBaseline="@id/baudrate_title" 
      android:layout_alignLeft="@id/can_speed" 
      android:entries="@array/baudrates" /> 
    </RelativeLayout> 

내가 관여 뷰 가능한 속성의 집합을 검색했지만 실마리를 찾을 수 없습니다. 아무도이 동작을 설명 할 수 있습니까? 나는 무엇을 간과 했는가? GridLayout이 방법은 더 나은 귀하의 요구에 맞게 것이 나에게 보인다

+0

@Ofek Ron이 말한 것과 같이 GridLayout을 사용해야합니다. – philip

답변

-1

...

+0

또는'TableLayout'. –

+0

GridLayout에는 RelativeLayout이 더 나은 선택이되어야하는 이유 중 하나 인 생각이 들었습니다. 뷰의 내용을 기반으로 관계를 설정하지는 않지만 주변 사각형 만 관계를 설정합니다.따라서 텍스트와 함께 다른 유형의 두보기를 정렬하는 기준선은 불가능합니다. TextView의 텍스트를 Spinner의 텍스트와 정렬 할 수 없습니다. 각각의 그리드 셀을 세로로 가운데 놓을 수는 있지만 Spinner에서 텍스트가 세로로 가운데에 있지 않기 때문에 기준선이 일치하지 않습니다. RelativeLayout에서는 "layout_alignBaseline"을 사용하여이 문제를 해결합니다. –

+0

지금까지 사용하지 않은 TableLayout을 사용해 보았습니다. GridLayout과 동일한 문제가 있다는 것을 알았습니다. 행 요소 정렬 기준선에 어떤 기능도 제공하지 않는 것으로 보입니다. 사실 GridLayout과 TableLayout이 개념적으로 거의 같은 것처럼 보입니다. –

2

는 예를 들어 2

로 열 개수와 이것에 대한 GridLayout를 사용하여 왼쪽 들어

<android.support.v7.widget.GridLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       app:alignmentMode="alignBounds" 
       app:columnCount="2" 
       app:orientation="vertical" 
       > 

편측 Tv + 콜론은 RelativeLayout 또는 심지어 LinearLayout을 사용하면 문제가 해결됩니다.

   <LinearLayout 
        android:gravity="center" 
        android:orientation="horizontal" 
        app:layout_column="0" 
        app:layout_columnWeight="1" 
        app:layout_gravity="fill_horizontal|fill_vertical" 
        app:layout_row="0"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         tools:text="Label" 
         android:maxLines="2" 
         android:ellipsize="end" 
         android:textColor="@color/black"/> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         tools:text=":" 
         android:textColor="@color/black"/> 
      </LinearLayout> 
+0

콜론에는 자체 TextView가 없으며 다른 TextView의 텍스트 일부입니다. 나는 이것을 조금 애매하게 말했어. 미안해. GridLayout을 시도 할 때 Spinner의 텍스트가 TextView보다 상당히 높게 표시됩니다. 내가 시도해도 layout_alignBaseline 태그가 없다고 생각하면 해결할 수 있습니다. –

관련 문제