私のアプリケーションではimageview
を持つtoolbar
を使用しています。その画像ビューを動的に変更する必要があります。ここでカスタムツールバーアイコンを動的に変更する方法
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/white"
android:id="@+id/toolbar"
app:theme="@style/ToolbarStyle" >
<ImageView
android:id="@+id/actionBarImage"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginRight="65dp"
android:src="@drawable/forewarn"/>
</android.support.v7.widget.Toolbar>
は活動のツールバーの宣言です:ここ
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final ImageView imageInToolbar = (ImageView) toolbar.findViewById(R.id.actionBarImage);
setSupportActionBar(toolbar);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setLogo(R.drawable.abc);
}
は私の実際のツールバーです:
私のここ
は私のtoolbar.xmlです必要なツールバーImageViewの動的変化:のGradleで
Glide.with(this)
.load(url)
.into(imageInToolbar);
:使用のため
:サーバからロード画像について
final ImageView imageInToolbar = (ImageView) toolbar.findViewById(R.id.actionBarImage);
setSupportActionBar(toolbar);
imageInToolbar.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.abc));
:
を試してみてください。そのimageview srcを変更する際の問題は何ですか? –
'imageInToolbar.setImageDrawable(ContextCompat.getDrawable(this、R.drawable.abc)); ' –
イメージソースを次のように取得しています:image_01.jpgイメージソースをロードし、ツールバーに適用する必要があります。 –