오늘이라도
[Android] 9. Layout Inflate 본문
반응형
https://github.com/upcake/Class_Examples
교육 중에 작성한 예제들은 깃허브에 올려두고 있습니다.
gif 파일은 클릭해서 보는 것이 정확합니다.
- Layout Inflate -
▼activity_main.xml : 메인 화면
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<Button
android:id="@+id/btnMain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="화면 인플레이트하기"
android:textSize="24sp"
android:layout_weight="10"/>
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="45" />
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="45" />
</LinearLayout>
▼sub1_layout.xml : 서브 화면 1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sub1 버튼" />
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
▼sub2_layout.xml : 서브 화면 2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="-1dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:text="sub2 버튼" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
app:srcCompat="@drawable/ic_launcher_foreground" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="463dp" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="318dp"
android:layout_height="91dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="240dp" />
</RelativeLayout>
▼MainActivity.java : 메인 화면 기능
package com.example.my09_layoutinflate;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//변수 선언
Button btnMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//변수 찾기
btnMain = findViewById(R.id.btnMain);
//기능 추가
btnMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout linear = findViewById(R.id.linear);
RelativeLayout relative = findViewById(R.id.relative);
//인플레이트
//Object 타입을 캐스팅하라는 에러가 나오는데 Alt + Enter로 쉽게 에러를 고칠 수 있다.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.sub1_layout, linear, true);
inflater.inflate(R.layout.sub2_layout, relative, true);
//서브 버튼 초기화화
Button btnSub1 = linear.findViewById(R.id.button1);
Button btnSub2 = relative.findViewById(R.id.button2);
//서브 버튼 기능 추가
btnSub1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "리니어 버튼 클릭", Toast.LENGTH_SHORT).show();
}
});
btnSub2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "렐러티브 버튼 클릭", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
반응형