공부일지
231115 (Android)
CD가참둥그렇다
2023. 11. 15. 13:41
중첩 분할하기
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal">
<LinearLayout
android:background="#7F9971"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:background="#A8617E"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:background="#30374B"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:background="#83B19E"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</LinearLayout>
</LinearLayout>
안드로이드 뷰의 속성
- xml의 id는 해당 xml 파일에서 중복 불가능하다.
- wrap content : 위젯이 가지는 기본 크기. 위젯의 자식 요소가 커지면 같이 커진다.
- 뷰의 간격 설정
- margin : 뷰의 바깥쪽 여백을 설정할 수 있다.
- padding : 뷰의 안쪽과 컨텐츠 사이의 여백을 설정할 수 있다.
- 뷰의 가시성(visibility)
- visible : 보인다.
- invisible : 공간을 차지하고 보이지 않는다.
- gone : 공간을 차지하지 않고 보이지 않는다.
- 그래비티 (gravity)
- 내부의 컨텐츠를 어떤 방향으로 밀착할 것인지 결정한다.
- bottom|right 처럼 | 기준으로 좌우에 여러 요소를 지정할 수 있다.
- layout_gravity : 부모 요소에서 나의 위치를 변경시킨다.
- relative layout에서 사용 불가능하다. 부모 기준으로 정렬 규칙이 이미 존재하기 때문이다.
- linear에서는 방향성에 따라서 좌우나 상하의 방향만 이동이 가능하다.
- 이미지 뷰
- android:src : 이미지를 불러올 위치를 설정한다.
- scale_type : 이미지를 채우는 방식을 결정한다.
- fitXY : 이미지를 늘려서 레이아웃에 채운다.
linear layout 테스트
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:background="#38D765"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:backgroundTint="#434243"
android:text="버튼 1"
android:layout_margin="5dp"
android:layout_width="200dp"
android:layout_height="match_parent"/>
<Button
android:backgroundTint="#757673"
android:text="버튼"
android:layout_margin="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"/>
<Button
android:backgroundTint="#757673"
android:text="버튼"
android:layout_margin="5dp"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="45dp"/>
</LinearLayout>
<LinearLayout
android:background="#C6DE27"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="top|left">
<Button
android:backgroundTint="#5C3001"
android:text="버튼"
android:layout_margin="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"/>
<Button
android:backgroundTint="#5C3001"
android:text="버튼"
android:layout_margin="5dp"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="45dp"/>
</LinearLayout>
<LinearLayout
android:background="#9B5D95"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="0dp">
<ImageView
android:src="@drawable/ic_launcher_foreground"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="버튼"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Button
android:text="버튼"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Button
android:text="버튼"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Button
android:text="버튼"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</LinearLayout>
<Button
android:backgroundTint="#EAE9E8"
android:textColor="#000"
android:layout_marginBottom="0dp"
android:text="버튼"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
프레임 레이아웃
- 여러 프레임을 전환하여 다양한 화면을 준비해둘 수 있다.
logcat 사용
- logd로 자동 완성하면 동작 로그를 기록할 수 있다.
- d : 개발자의 로그
- e : 에러 발생의 로그
버튼을 눌러 사진을 순환하는 앱
btn_change = findViewById(R.id.btn_change);
imgv1 = findViewById(R.id.imgv1);
imgv2 = findViewById(R.id.imgv2);
imgv3 = findViewById(R.id.imgv3);
btn_change.setOnClickListener(v->{
if(imgv1.getVisibility()== View.VISIBLE){
imgv1.setVisibility(View.GONE);
imgv2.setVisibility(View.VISIBLE);
Log.d("CD log", "이미지 변경 ->2");
} else if (imgv2.getVisibility()==View.VISIBLE){
imgv2.setVisibility(View.GONE);
imgv3.setVisibility(View.VISIBLE);
Log.d("CD log", "이미지 변경 ->3");
} else if (imgv3.getVisibility()==View.VISIBLE){
imgv3.setVisibility(View.GONE);
imgv1.setVisibility(View.VISIBLE);
Log.d("CD log", "이미지 변경 ->1");
}
});