[Android] μλλ‘μ΄λ - ActionBar menu μ¬μ©νλ λ°©λ²
Action Barλ μλλ‘μ΄λ μ±μμ νλ©΄ μλ¨μ μλ μ€μν UI μμμ΄λ€.
μ£Όλ‘ μ±μ μ λͺ©, μ‘μ
λ²νΌ, νμ κΈ°λ₯ λ±μ ν¬ν¨νκ³ μμ΄ μ¬μ©μμκ² μ±μ κΈ°λ₯κ³Ό λ΄λΉκ²μ΄μ
μ μ 곡νλ€.
1. res-values-themes-themes.xml μμ
μ€νμΌ νκ·Έμμ parent μμ±μ
parent="Theme.MaterialComponents.DayNight.DarkActionBar" λ‘ λ°κΏμ€λ€.
λμ΄νΈ λ²μ λ λκ°μ΄ μμ νλ€.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.ActionBar" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.ActionBar" parent="Base.Theme.ActionBar" />
</resources>
2. res-menu 리μμ€ λλ ν 리 μμ±
3. main.xml λ©λ΄ μμ΄ν μΆκ°
λ©λ΄ μμ΄ν 리μ€νΈλ₯Ό μΆκ°νλ€.
μ¬λ¬κ° μΆκ°ν μ μλ€.
4. Common Attributes μμ
id, title, icon, showAsAction λ±μ μμ νλ€.
κ·ΈλΌ μλμ κ°μ΄ μμ΄μ½μ΄ μμ±λ κ²μ νμΈν μ μλ€.
5. MainActivity.java
- onCreate ν¨μμμ νμ΄νμ μ€μ νλ€.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("ν¬μ€ν
리μ€νΈ");
}
- μ‘μ λ°μ λ©λ΄ μμ΄μ½μ΄ λμ€λλ‘ μ€μ νλ ν¨μ onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
- μ‘μ λ°μ λ©λ΄ μμ΄μ½ ν΄λ¦νλ©΄, λμνλ ν¨μ onOptionsItemsSelected
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == R.id.menuAdd){
Intent intent = new Intent(MainActivity.this, AddActivity.class);
launcher.launch(intent);
}
return super.onOptionsItemSelected(item);
}