앱 서비스를 제작하다 보면 팝업창, 다이얼로그를 만들어야하는 경우가 많다!
오늘은 안드로이드 앱 내에서 원하는 크기의 팝업창, 다이얼로그를 만들기!
다양한 방법이 많지만 내가 자주 사용하는 방법은 아래와 같다.
1. 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 26 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com......"> <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=".Home.MainActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".PopActivity" android:theme="@style/NoTitleBarDialog" /> </application> </manifest> |
분명히 (줄22, 23) 빨간줄이 그러질거에요 당황하지말고 아래 따라하기
2. PopActivity를 생성해준다. 그 이후 다음과 같이 추가해준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class PopActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pop); Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int width = (int) (display.getWidth() * 0.9); //Display 사이즈의 90% 각자 원하는 사이즈로 설정하여 사용 int height = (int) (display.getHeight() * 0.9); //Display 사이즈의 90% 각자 원하는 사이즈로 설정하여 사용 getWindow().getAttributes().width = width; getWindow().getAttributes().height = height; } } | cs |
3. 스타일을 추가한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="NoTitleBarDialog" parent="Theme.AppCompat.Light.Dialog"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">#07000000</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> </resources> | cs |
4. layout 은 activity_pop의 이름으로, 내용은 알아서 작성해주세요 ㅎ
완성!
장점:
모든 기기에 디스플레이에 같은 %의 사이즈로 팝업창을 생성할 수 있음
'ANDROID - JAVA' 카테고리의 다른 글
[ANDROID - JAVA] GMT 시간 TimeZone 설정하기 (0) | 2019.03.15 |
---|---|
[ANDROID - JAVA] 커스텀 Listview 만들기 (0) | 2019.03.14 |
[ANDROID - JAVA] 디바이스에 데이터 저장하기 (SharedPreferences 사용하기) (0) | 2019.01.19 |
[ANDROID - JAVA] Picasso 이미지 라이브러리 사용하기 (URL로 이미지 보여주기) (0) | 2019.01.17 |
[ANDROID - JAVA] 여러개의 버튼 각각 다른 이벤트 주기 (0) | 2019.01.15 |