View & ViewGroup

채현·2023년 2월 13일
0

안드로이드

목록 보기
4/37

🧰 CompoundButton

기본 컴파운드버튼의 크기 wrap_content 사용 (안드로이드 권장)

💬 CheckBox (multiple choice)

<CheckBox
		android:id="@+id/apple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apple"
        android:checked="true"/>

기본적으로 체크되어있는 상태 : android:checked="true"
(java에 쓸 경우 --> apple.setChecked(true);)

체크된 상태를 확인하는 리스너 : CompoundButton.OnCheckedChangeListener

💬 ToggleButton / Switch

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="켜짐"
        android:textOff="꺼짐"/>

토글버튼의 on/off 상태 텍스트 변경 textOn /textOff 이용

<Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="알람허용"/>

ToggleButton / Switch 모두 OnCheckedChangeListener 사용하여 on/off 상태의 메소드 작성 가능

💬 RadioButton (single choice)

혼자 사용되지 않고 LinearLayout을 상속받은 RadioGroup(orientation : V/H 배치)으로 묶어서 사용

<RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_kor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="KOREA"
            android:checked="true"/>

        <RadioButton
            android:id="@+id/rb_chi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CHINA"/>

        <RadioButton
            android:id="@+id/jpn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="JAPAN"/>

</RadioGroup>

RadioButton의 체크상태가 변경될 때마다 반응하는 리스너는 버튼들에 붙이지 말고 RadioGroup에 붙여서 한 번에 제어하는 것을 권장
.getCheckedRadioButtonId() 을 이용하여 RadioGroup의 체크된 버튼 얻어오기

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //두 번째 파라미터 i : 선택된 RadioButton의 id 값
                RadioButton rb = findViewById(i);
                tv.setText(rb.getText().toString());
            }
});


🔗 RelativeLayout

alignParentRight : 부모를 기준으로 오른쪽에 위치
alignRight : 다른 뷰를 기준으로 오른쪽을 맞춤

centerVertical : 세로의 중앙에 위치

below : 다른 뷰 기준의 아래
above : 다른 뷰 기준의 위

toRightOf : 다른 뷰의 오른쪽에 위치

rigt, left, Horizontal 등 변경해서 사용

Button과 View들의 우선순위
: 원래는 나중에 작성한 뷰가 기존 뷰를 덮어야 하지만, Button은 클릭되는 뷰이기때문에 순서에 상관없이 앞으로 놓여짐


🔄 FrameLayout

겹치는 것 외에는 기능 없음
탭 버튼을 눌렀을 때 나오는 화면 제각각, 그 화면마다 버튼을 생성하는 것 어려움
--> layout을 여러 개 준비해서 겹쳐 놓고 안보이게 visibility를 이용, 버튼을 누를 때마다 visibility의 상태를 변화
(RelativeLayout으로도 만들 수는 있지만 많은 기능을 가지고 있어 무거움, FrameLayout으로 만드는것이 효율적 탭 만들때 권장)


📉 TableLayout

width/height 자동으로 warp_content

BUT 단점...!
ⓐ column span만 가능 Row span 불가
(TableRow로 나누어져있어 세로줄 병합이 불가)
ⓑ 중첩구조

<TableLayout><TableRow></TableRow></TableLayout>

📈 GridLayout

TableLayout의 단점을 개선한 GridLayout(격자배치)
TableLawidth/height 자동으로 warp_content : 생략가능

android:orientation="horizontal" --> android:columnCount="3"
android:orientation="vertical" --> android:rowCount="3"

columnCount를 이용하여 칼럼 갯수를 정하고 Exel 시트처럼 column/row 위치를 지정 및 병합 가능
병합시, content의 사이즈는 같이 커지지 않고 그만큼의 공간만 차지
--> fill_horizontal : content가 자리를 차지한 만큼 채워짐

🚨 Exel처럼 다른 뷰들의 행과 열 사이즈에 따라 사이즈가 변동됨


💟 ConstraintLayout

ConstraintLayout의 필요성
ⓐ alignLeft = "@id/b5" : 해당 뷰의 오른쪽 기준 left? 왼쪽 기준 left? --> 모호해용!
ⓑ Design mode의 사용성 개선

제약조건을 가지고 화면 배치
x축과 y축 설정하지 않으면 ERROR
constraintRight_toLeftOf="@id/btn" : 해당 content의 오른쪽을 id btn의 왼쪽에!

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

가운데 배치 center 없음 --> 상충되는 조건을 주면 가운데에 배치됨
(왼쪽에도 붙고 오른쪽에도 붙고 위에도 붙고 아래도 붙여! --> 정중앙)

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

layout_constraintHorizontal_bias 이용하여 퍼센트 주기

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        app:layout_constraintTop_toBottomOf="@id/b4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.4"/>

☝ ScrollView

✌ ScrollView에 자식View를 배치할때 절대 규칙 두 가지

① ScrollView can host only one direct child
--> ScrollView 안에는 자식View 오직 한 개만 배치 가능 ( ViewGroup 이용)
② 높이값은 무조건 wrap_content

가로 스크롤은 불가능 --> HorizontalScrollView : 가로스크롤용 ScrollView
마찬가지로 자식View 1개만 가능, 반대로 너비가 무조건 wrap_content!

가로세로 모두 스크롤 사용하고 싶을 경우 (eg. Image file...)
--> ScrollView와 HorizontalScrollView 중첩으로 이용

sv.fullScroll(ScrollView.FOCUS_DOWN); 이용하면 한 번에 제일 아래로 스크롤을 내리게 할 수 있음

0개의 댓글