是否可以将彩色图标添加到透明工具栏?
Is adding a colored icon to a transparent toolbar possible?
是否可以将这样的圆形图标添加到透明工具栏(样式类似于操作栏)并保持其颜色?
编辑:此图标将在工具栏上右对齐,类似于您在大多数应用程序中看到三点菜单图标的位置。
您想为工具栏扩充菜单,将背景设置为透明,用图标扩充菜单。
Activity 与工具栏
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getSupportActionBar().hide();
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Hello World");
toolbar.inflateMenu(R.menu.menu_main);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
// Your action here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
}
activity布局
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="56dp"
android:layout_width="match_parent"
app:theme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="5dp"
android:background="@android:color/transparent" />
</LinearLayout>
菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/icon"
android:title="Icon Title"
android:icon="@drawable/ic_launcher"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
是否可以将这样的圆形图标添加到透明工具栏(样式类似于操作栏)并保持其颜色?
编辑:此图标将在工具栏上右对齐,类似于您在大多数应用程序中看到三点菜单图标的位置。
您想为工具栏扩充菜单,将背景设置为透明,用图标扩充菜单。
Activity 与工具栏
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getSupportActionBar().hide();
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Hello World");
toolbar.inflateMenu(R.menu.menu_main);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
// Your action here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
}
activity布局
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="56dp"
android:layout_width="match_parent"
app:theme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="5dp"
android:background="@android:color/transparent" />
</LinearLayout>
菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/icon"
android:title="Icon Title"
android:icon="@drawable/ic_launcher"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>