2012-02-06 3 views
1

고객 정보를 얻으려는 활동이 있습니다. 첫 번째 줄에는 이름, 두 번째 줄 주소, 세 번째 줄은 도시, 주 및 우편 번호입니다. RelativeLayout을 사용하고 android : layout_below 및 android : layout_toRightOf를 사용하여 원하는 것을 얻을 수 있습니다. 그런 다음 내보기의 너비를 구체적으로 지정해야합니다. 사용자가 새로운 버전의 안드로이드 버전을 재배치하거나 전환 할 때 그 너비가 이상하게 보일 수 있습니다. LinearLayout의 가중치 기능을 활용하길 원하지만 모든 것이 한 줄에 모두 표시됩니다. LinearLayout 내에 뷰의 행을 만들기 위해 어떻게 라인 브레이크를 삽입하나요?Android LinearLayout XML의 줄 바꿈

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
android:id="@+id/custLabel" 
android:layout_height="wrap_content" 
android:layout_weight=".25" 
android:layout_width="0dip" 
android:text="Name"/> 
<EditText 
android:id="@+id/customer" 
android:layout_height="wrap_content" 
android:layout_weight=".75" 
android:layout_width="0dip"/> 
<TextView 
android:id="@+id/addressLabel" 
android:layout_height="wrap_content" 
android:layout_weight=".25" 
android:layout_width="0dip" 
android:text="Address"/> 
<EditText 
android:id="@+id/address" 
android:layout_height="wrap_content" 
android:layout_weight=".75" 
android:layout_width="0dip"/> 
<TextView 
android:id="@+id/cityLabel" 
android:layout_height="wrap_content" 
android:layout_weight=".1" 
android:layout_width="0dip" 
android:text="City"/> 
<EditText 
android:id="@+id/city" 
android:layout_height="wrap_content" 
android:layout_weight=".25" 
android:layout_width="0dip"/> 
<TextView 
android:id="@+id/stateLabel" 
android:layout_height="wrap_content" 
android:layout_weight=".1" 
android:layout_width="0dip" 
android:text="State"/> 
<EditText 
android:id="@+id/state" 
android:layout_height="wrap_content" 
android:layout_weight=".20" 
android:layout_width="0dip"/> 
<TextView 
android:id="@+id/zipLabel" 
android:layout_height="wrap_content" 
android:layout_weight=".1" 
android:layout_width="0dip" 
android:text="Zip"/> 
<EditText 
android:id="@+id/zip" 
android:layout_height="wrap_content" 
android:layout_weight=".25" 
android:layout_width="0dip"/> 
</LinearLayout> 

답변

4

이렇게하려면 내포 된 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" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/custLabel" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" 
      android:text="Name" /> 

     <EditText 
      android:id="@+id/customer" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".75" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/addressLabel" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" 
      android:text="Address" /> 

     <EditText 
      android:id="@+id/address" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".75" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/cityLabel" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".1" 
      android:text="City" /> 

     <EditText 
      android:id="@+id/city" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" /> 

     <TextView 
      android:id="@+id/stateLabel" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".1" 
      android:text="State" /> 

     <EditText 
      android:id="@+id/state" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".20" /> 

     <TextView 
      android:id="@+id/zipLabel" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".1" 
      android:text="Zip" /> 

     <EditText 
      android:id="@+id/zip" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight=".20" /> 
    </LinearLayout> 
</LinearLayout> 

이 작동해야합니다.

+0

그건 작동합니다. 더 간단한 방법이 없다는 것을 믿을 수 없습니다. 감사! – jmease

1

하나의 LinearLayout으로 원하는 작업을 수행 할 수 없습니다. TableLayout을 확인해야합니다.

+0

내가 사용했습니다 TableLayouts :

다음

은 예입니다. 열 너비를 백분율로 설정할 수 있습니까? – jmease

+0

"layout-weight"속성을 사용하면 퍼센트를 거의 같게 설정된 양의 공간을 사용하도록 열을 설정할 수 있습니다. –

3

선형보기의 속성은 수평 또는 수직 방식으로 자식보기를 정렬합니다. 대체 텍스트로 각 텍스트보기 및 편집 텍스트를 주문할 수 없습니다. 이를 위해서는 표 레이아웃이나 현재 선형 레이아웃 내부의 다른 선형 레이아웃과 같은 추가 레이아웃을 사용해야합니다. 다른 것들에 대한

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

<TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight=".25" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/custLabel" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" 
      android:text="Name" /> 

     <EditText 
      android:id="@+id/customer" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".75" > 

      <requestFocus /> 

     </EditText> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/addressLabel" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" 
      android:text="Address" /> 

     <EditText 
      android:id="@+id/address" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".75" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/cityLabel" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".1" 
      android:text="City" /> 

     <EditText 
      android:id="@+id/city" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/TableRow4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/stateLabel" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".1" 
      android:text="State" /> 

     <EditText 
      android:id="@+id/state" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".20" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/zipLabel" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".1" 
      android:text="Zip" /> 

     <EditText 
      android:id="@+id/zip" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" /> 
    </TableRow> 

</TableLayout>