Vintage appMaker의 Tech Blog

화면 회전시 Layout 변경 본문

Source code or Tip/Android(Java, Kotlin)

화면 회전시 Layout 변경

VintageappMaker 2021. 5. 22. 13:16

 

[activity_main.xml]

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextClock
        android:textSize="48dp"
        android:gravity="center"
        android:id="@+id/clock"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextClock
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:background="#343434"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="Text1"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <TextView
        android:textSize="28dp"
        android:gravity="center"
        android:text="Text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn1"
        android:text="Button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:text="Button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:text="Button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</LinearLayout>

1. Activity의 android:configureChanges를 설정한다.

<activity android:name=".MainActivity" android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

 

2. Activity의 onConfigurationChanged를 구현한다.

    override fun onConfigurationChanged(newConfig: Configuration) {

        val clock = findViewById<TextClock>(R.id.clock)
        val btn1  = findViewById<Button>(R.id.btn1)
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            layoutToggle(listOf(clock, btn1), true)
        } else{
            layoutToggle(listOf(clock, btn1), false)
        }

        super.onConfigurationChanged(newConfig)
    }

3. 화면변경시(landscape), View의 ID 값을 부여하고 Visibility를 설정하는 경우도 있지만 의미없는 View까지 ID를 부여해야 하는 부담과 단지 한 두 View만 보여주는 경우는 다음과 같이 code로 ChildView를 제어하는 경우를 사용한다. 

 

    fun layoutToggle(unchangeList : List<View>, bToggle : Boolean) {
        fun changeLayout(b :Boolean) {
            val parentV = unchangeList[0].parent
            if (parentV !is ViewGroup) return

            for (i in 0 until parentV.childCount) {
                parentV
                        .getChildAt(i)
                        .takeIf { !( it in unchangeList)  }
                        ?.visibility = if (b) View.GONE else View.VISIBLE
            }
        }

        val mainLooper   = Looper.getMainLooper()
        val isMainLooper = Looper.myLooper() == mainLooper

        if (isMainLooper) {
            changeLayout(bToggle)
        } else {
            val handler = Handler(mainLooper)
            handler.post( { changeLayout(bToggle) } )
        }

    }
Comments