레이아웃에 공백 추가


79

나는 안드로이드 내에서 빈 줄을 만들려고합니다. 이것이 내가하고있는 일입니다.

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="\n\n"

더 나은 방법이 있는지 알고 싶습니다. 감사


1
심? 여백? 개별 레이아웃에 특정한 문제이므로 전체 레이아웃을 보는 데 도움이됩니다.
Glendon Trullinger 2011-06-15

답변:


171

사용 Space또는 View공간의 특정 금액을 추가 할 수 있습니다. 30 개의 수직 밀도 픽셀의 경우 :

<Space
  android:layout_width="1dp"
  android:layout_height="30dp"/>

유연한 공간 채우기가 필요한 경우 다음 View항목간에 사용 하십시오 LinearLayout.

<View
  android:layout_width="1dp"
  android:layout_height="match_parent"
  android:layout_weight="1"/>

또는

<View
  android:layout_width="1dp"
  android:layout_height="0dp"
  android:layout_weight="1"/>

이는 위젯 ( FrameLayout대신 사용)을 제외한 대부분의 API 14 이상의 레이아웃에서 작동합니다 .

[Space를 사용하기 위해 2014 년 9 월에 업데이트되었습니다. @Sean에게 모자 팁]


fill_parent는 더 이상 사용되지 않으므로 match_parent로 업데이트했습니다. 또한 대부분의 경우 0dip이 match_parent보다 권장됩니다.
radley

이를 프로그래밍 방식으로 달성하는 방법이 있습니까? LinearLayout과 동일하게 작동하지만 LinearLayout을 View로 대체합니까?
Musterknabe 2013 년

이 여분의 공간을 채우는 생성으로 감사합니다, 나는 공간을 사용
Naveed 아마드을

이전 버전과 호환되는 공간의 경우 <android.support.v4.widget.Space
Hibbem

28

간격이 정확히 2 줄 높이 일 필요가없는 경우 다음과 같이 빈 뷰를 추가 할 수 있습니다.

    <View
        android:layout_width="fill_parent"
        android:layout_height="30dp">
    </View>

@wizurd 위젯은 View를 사용할 수 없으며 View 대신 FrameLayout을 사용합니다.
radley

줄 높이가 텍스트 크기에 따라 달라 지므로 정확히 두 줄이되지는 않습니다 .
Marcel Bro

네. 그게 내 대답에서 말한 것입니다.
캐스퍼 하머

18

보기 당신은 배경 색상을 변경, 필요하다면 공간 이 아니라면.

즉, 뷰 배경을 변경해야 함을 의미합니다.

<View
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:background="@color/YOUR_BACKGROUND">
</View>

또는 공간

<Space
        android:layout_width="match_parent"
        android:layout_height="20dp"
         />

3
View 사용시기와 Space 사용시기에 대한 힌트는 배경색을 표시 할 수없는 문제를 해결했습니다. 감사!
Igor

1
감사합니다!!! 공간의 배경색이 궁금합니다.
Sam

16

업데이트 된 답변 : API 14부터 문서에 설명 된대로 "Space"View를 사용할 수 있습니다 .

공간은 범용 레이아웃에서 구성 요소 사이에 간격을 만드는 데 사용할 수있는 경량 View 하위 클래스입니다.


3
궁금한 사람을위한 예 :<Space android:layout_width="fill_parent" android:layout_height="10dp" />
XåpplI'-I0llwlg'I-

5

레이아웃 사이에 공간을 제공하려면 공간을 사용하는 방법입니다. 여백을 제거하면 표시되지 않습니다. 표시 할 공간 내부의 텍스트를 사용하는 것은 좋은 방법이 아닙니다. 도움이 되길 바랍니다.

<Space
        android:layout_width="match_content"
        android:layout_height="wrap_content"
        android:layout_margin="2sp" />

1
매우 가벼운 뷰이므로 View 대신 Space를 사용해야합니다.
Bhawna Raheja 2015

3
<View
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="#80000000">
</View>

3

나는 CaspNZ의 접근 방식에 매우 동의하지 않습니다.

우선,이 보이지 않는 뷰는 "fill_parent"이기 때문에 측정됩니다. Android는 올바른 너비를 계산하려고합니다. 대신 여기에 작은 상수 (1dp)가 권장됩니다.

둘째, View는 가장 빠른 속도를 위해 UI 구성 요소 사이에 빈 공간을 만드는 전용 클래스 인 더 간단한 클래스 Space로 대체되어야합니다.


2

이 시도

layout.xml에서 :

<TextView
        android:id="@+id/xxx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/empty_spaces" />

strings.xml에서 :

<string name="empty_spaces">\t\t</string>

그것은 나를 위해 일했다


2

이것은 공간 이나 보기 를 사용하여 달성 할 수 있습니다 .

공간 은 가볍지 만 그다지 유연하지는 않습니다.

View 는 화면에서 직사각형 영역을 차지하며 그리기 및 이벤트 처리를 담당합니다. 보기는 더 사용자 정의가 가능하며 배경을 추가하고 공간과 같은 모양을 그릴 수 있습니다.

공간 구현 :

(예 : 세로 20 개 및 가로 10 개 밀도 픽셀을위한 공간)

<Space
  android:layout_width="10dp"
  android:layout_height="20dp"/>

구현보기 :

(예 : 배경색을 포함하여 세로 20 개 및 가로 10 개 밀도 픽셀에 대한보기)

  <View
       android:layout_width="10dp"
       android:layout_height="20dp"
       android:background="@color/bg_color"/>

HTML 엔티티를 사용하는 문자열 형식화를위한 공간 :

&#160;깨지지 않는 공백을 위해. &#032;일반 공간을 위해.


1

1 에 weightSum태그를 추가 linearlayout하고 그 아래에 해당하는 뷰에 대해 layout_weight.9를 제공 하면 뷰 사이에 공간이 생깁니다. 값을 실험하여 적절한 값을 얻을 수 있습니다.


0

모든 답변에 동의합니다 ...... 또한,

    <TextView android:text=""
              android:layout_width="match_parent"
              android:layout_height="30dp"
              android:layout_weight="2" />

작동해야합니다 :) TextView가 내가 가장 좋아하는 (메모리 낭비!)


0

내 경우에는 이전 답변이 작동하지 않았습니다. 그러나 메뉴에 빈 항목을 만들면됩니다.

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
  ...
  <item />
  ...
</menu>

0

이 작업을 수행하는 더 좋은 방법이 있습니다. 아래 코드를 사용하고 원하는 빈 영역의 크기에 따라 변경하십시오.

     <Space
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_column="0"
            android:layout_row="10" />

그리드 레이아웃과 함께 사용하는 경우

 android:layout_row="10" />

0

다음은 줄 크기로 빈 줄을 만드는 간단한 방법입니다. 여기에서 빈 줄의 크기를 조정할 수 있습니다. 이걸로 해봐.

           <TextView
            android:id="@id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="5dp"/>
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.