android Toolbar 의 간단 한 사용
도구 모음 에 사용 할 표준 도구 모음;Action Bar 대신 사용 하기;
ToolBar 를 사용 하려 면 Activity 설정 theme 에서 ActionBar 를 제거 해 야 합 니 다.예 를 들 어 사용 합 니 다.
Theme.AppCompat.Light.NoActionBar
또는 테마 style 에서 스타일 사용자 정의:
<style name="AppThemeNoColour" parent="Theme.AppCompat.Light.NoActionBar">
<!-- ActionBar title; , -->
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<!-- -->
<item name="colorPrimaryDark">@android:color/black</item>
<!-- -->
<item name="android:windowBackground">@android:color/white</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
다음은 레이아웃 을 살 펴 보 겠 습 니 다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="www.toolbar.com.toolbardemo.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="56dp"
android:id="@+id/toolbar"
android:background="@android:color/holo_blue_dark"
/>
</LinearLayout>
Toolbar 는 리 턴 키,아이콘,제목,부제,메뉴 를 설정 할 수 있 습 니 다.
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
// Toolbar ActionBar
setSupportActionBar(toolbar);
//
toolbar.setLogo(R.mipmap.ic_launcher);
//
getSupportActionBar().setTitle("ZhengDang");
//
toolbar.setSubtitle("2015.01.13");
//
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
리 턴 키 는 감청 설정 도 가능 합 니 다:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
}
});
설정 메뉴 는 먼저 menu 레이아웃 을 사용자 정의 해 야 합 니 다:res--->menu 에서 xml 파일 을 만 듭 니 다:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--
showAsAction:
ifRoom
Item , 4 4 Item 。
4 ,
never
。 , , item , 。
always
, 。
withText
withText Action bar 。Action bar ,
, Action bar , 。
collapseActionView
, ,
。 , , ,
。
-->
<item
android:id="@+id/action_setting"
android:orderInCategory="100"
android:title="settings"
app:showAsAction="always"
android:icon="@drawable/scan_bg"
/>
<item
android:id="@+id/action_ufc"
android:orderInCategory="100"
android:title="UFC"
/>
<item
android:id="@+id/action_wlf"
android:orderInCategory="100"
android:title=" "
/>
<item
android:id="@+id/action_klf"
android:orderInCategory="100"
android:title=" "
/>
</menu>
그리고 MainActivity 에서 메뉴 단 추 를 설정 합 니 다.
/**
* :
* , menu Menu 。 true menu,false ;
* ( )
*/
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
// :
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.action_wlf:
Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
break;
case R.id.action_klf:
Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
break;
case R.id.action_ufc:
Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
break;
case R.id.action_setting:
Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
클릭 하여 링크 열기 무료원본 코드 다운로드이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.