안드로이드 스튜디오 ToolBar 적용하기

계발에서 개발까지 

 

 

 

Android Studio Toolbar

 

툴바적용하기

 

 

res - values - styles

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

 

툴바에 사용할 옵션의 아이템을 설정해 주기 위해서 

 

res - menu 폴더를 생성

 

생성 이후 에 밑에 소스를 넣어주세요.

    <item
        android:id="@+id/action_settings1"
        android:orderInCategory="100"
        android:title="text1"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_settings2"
        android:orderInCategory="100"
        android:title="text2"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_settings3"
        android:orderInCategory="100"
        android:title="text3"
        app:showAsAction="never" />

 

 activity_main_xml

 

  <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/img_upperbar"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    </androidx.appcompat.widget.Toolbar>

MainActivity.java 

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(""); //타이틀 없음

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings1:
                Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }
}

 

수고하셨습니다 ★

더 많은 정보 https://deumdroid.tistory.com/ 

댓글

Designed by JB FACTORY