android 去掉工具栏,android – 如何删除默认的工具栏菜单项并替换为不同的图标?...

这就是我使用工具栏的方式.它是一个独立的工具栏,可让您更好地控制其在类中的功能.

activity_home.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/tool_bar"

android:layout_height="56dp"

android:layout_width="match_parent"

android:elevation="5dp"

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

android:background="@color/primary" />

android:layout_below="@+id/tool_bar"

android:layout_width="match_parent"

android:layout_height="match_parent">

样式

@color/primary

@color/secondary

@color/black

@null

true

false

true

true

true

** MyActivity **

public class MyActivity extends ActionBarActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_home);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

// Set navigation icon

toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_launcher));

// Navigation onClickLister

toolbar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// finish(); // or your action here

}

});

//Inflate menu

toolbar.inflateMenu(R.menu.menu_my);

// menu items

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

@Override

public boolean onMenuItemClick(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {

// Your action here

return true;

}

return false;

}

});

// Clear toolbar icons

toolbar.getMenu().clear(); // I believe this is what you are looking for

// Set title

toolbar.setTitle("Title");

//Set SubTitle

toolbar.setSubtitle("Sub Title");

}

}

更改工具栏内所有内容的工作示例

布局

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/toolbar"

android:layout_height="56dp"

android:layout_width="match_parent"

android:elevation="5dp"

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

android:background="@color/primary" />

android:layout_below="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="16dp"

android:text="Change Menu "

android:gravity="center_vertical"

android:id="@+id/swChaneMenu" />

菜单1

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools">

android:id="@+id/toast"

android:title="Toast"

android:orderInCategory="100"

android:icon="@drawable/ic_done_black"

app:showAsAction="always" />

菜单2

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools">

android:id="@+id/action_settings"

android:title="@string/action_settings"

android:orderInCategory="100"

app:showAsAction="never" />

活动

public class TestingActionBar extends ActionBarActivity {

SwitchCompat swChaneMenu;

Toolbar toolbar;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_testing_action_bar);

toolbar = (Toolbar) findViewById(R.id.toolbar);

toolbar.setTitle("Toolbar Test 1");

toolbar.inflateMenu(R.menu.menu_testing_action_bar);

toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.nav_tint));

toolbar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

@Override

public boolean onMenuItemClick(MenuItem item) {

int id = item.getItemId();

if (id == R.id.toast) {

Toast.makeText(TestingActionBar.this, "Toasty", Toast.LENGTH_SHORT).show();

return true;

}

if (id == R.id.action_settings) {

Toast.makeText(TestingActionBar.this, "Settings", Toast.LENGTH_SHORT).show();

return true;

}

return false;

}

});

swChaneMenu = (SwitchCompat) findViewById(R.id.swChaneMenu);

swChaneMenu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {

toolbar.setTitle("Toolbar Test 2");

toolbar.getMenu().clear();

toolbar.inflateMenu(R.menu.menu_settings);

toolbar.setNavigationIcon(null);

toolbar.setBackgroundColor(getResources().getColor(R.color.red));

} else {

toolbar.setTitle("Toolbar Test 1");

toolbar.getMenu().clear();

toolbar.inflateMenu(R.menu.menu_testing_action_bar);

toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.nav_tint));

toolbar.setBackgroundColor(getResources().getColor(R.color.primary));

}

}

});

}

}

未检查开关

切换检查

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值