LinearLayout은 뷰그룹으로 다른 뷰나 뷰그룹을 포함할 수 있다.
LinearLayout은 뷰들이 차례대로 배치되는 레이아웃으로 수평(horizontal)또는 수직(vertical)방향으로 설정할 수 있다.
<LinearLayout
xmlns:android="<http://schemas.android.com/apk/res/android>"
xmlns:tools="<http://schemas.android.com/tools>"
xmlns:app="<http://schemas.android.com/apk/res-auto>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>
xml코드로는 위와 같이 작성하며, layout_width
, layout_height
, orientation
은 필수요소이다.
이제 LinearLayout안에 TextView를 추가해 보자.
텍스트뷰는 Design탭 이나 Text(Code)탭에서 추가 할 수 있다.
<!-- activity_main.xml -->
<TextView
android:id="@+id/name_text"
android:textSize="@dimen/text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/name"
android:textAlignment="center" />
<!-- dimens.xml -->
<resources>
<dimen name="text_size">20sp</dimen>
</resources>
<!-- string.xml -->
<resources>
<string name="app_name">AboutMe</string>
</resource>
텍스트뷰에 textSize
를 사용할 때 크기는 sp
지정해주어야 해상도의 영향을 덜 받는다. 그리고 사이즈의 경우 dimens.xml
에 값을 저장해두고 사용하는 것이 권장된다.
또 text
옵션을 설정할 때도 string.xml
에 저장해두고 사용하는 것을 권장한다.
뷰에는 padding
이랑 margin
이라는 속성이 있다.
padding
은 뷰의 경계선에서 뷰의 내용물사이에 공백을 생성하는 것이다.
margin
은 뷰의 경계선과 다른 뷰 또는 부모 뷰,뷰그룹과의 공백을 만드는 것이다.
padding