본문 바로가기

카테고리 없음

Android / Java) 키보드 위에 레이아웃 올리기


카카오톡(KakaoTalk)에서 글을 쓰는 구간인 EditText에 Focus가 활성화되어있으면 이렇게 EditText레이아웃이 키보드 위로 올라오게 된다.




키보드가 올라오자 입력값을 전달할 레이아웃도 같이 올라온 모습이다.


위같은 방법을 사용하기위해서는 아래의 코드를 보아야 한다.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>
 
cs




위의 코드에서 하나 특이한 점이 있다.


레이아웃 안에 오직 LinearLayout만 1의 weight값을 준것이다.


저렇게 하면 밑에있는 EditText를 눌러 포커스를 지정했을때 키보드 위로 EditText가 얹혀지게 된다.



하지만 어딘가 불안정하게 얹히는것을 볼 수 있는데,


이걸 해결하기위해 매니페스트(manifest)의 해당 액티비티에 코드 하나만 집어넣으면 된다.







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bino.ranpick">
 
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Activity.Splash_Activity">
    </activity>
    <activity
    android:name=".Activity.Main_Activity"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
</application>
 
</manifest>
cs




해당 액티비티에 위의 17번 행처럼


    android:windowSoftInputMode="adjustResize"


라는 코드를 넣어주면


옵션 이름처럼 키보드 레이아웃에 맞춰서 Resizing해준다.