위에 캡쳐본 처럼 우상단"옵션메뉴"를 만들고 싶으면 onCreateOptionMenu()메서드를 오버라이딩해서 Menu를 구성하면 된다.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//res/menu/menu_main.xml문서를 활용해서 메뉴 아이템 구성하기
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
menu_main.xml의 코드를 살펴보면 아래와 같다.
<menu 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"
tools:context="com.example.step05fragment2.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
직접 드래그를 통해서 다른 Menu들도 추가할 수 있다.
showAsAciton은 ActionBar에 보이게 할 것인지 말것인지를 설정해주는 것이다.
never외에도
always, collapseActionView, ifRoom등등이 있다.
app:showAsAction=""/>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//res/menu/menu_main.xml문서를 활용해서 메뉴 아이템 구성하기
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//클릭한 아이템의 아이디가 리턴된다.
int id = item.getItemId();
//아이디를 이용해서 분기하기
if (id == R.id.action_settings) {
return true;
}else if(id==R.id.one){
}else if(id==R.id.two){
}
return super.onOptionsItemSelected(item);
}
프래그먼트도 생명주기가 있다.
프래그먼트가 더이상 필요없을 때 bindin에 null을 부여해주면된다.
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
'수업내용' 카테고리의 다른 글
20230731 Kotlin (0) | 2023.07.31 |
---|---|
20230728 Kotlin (0) | 2023.07.28 |
20230727 Kotlin Function (0) | 2023.07.27 |
20230726 Android Fragment (0) | 2023.07.26 |
20230726 Kotlin (0) | 2023.07.26 |
댓글