Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从url图片设置徽标工具栏_Android_Toolbar_Android Toolbar - Fatal编程技术网

Android 从url图片设置徽标工具栏

Android 从url图片设置徽标工具栏,android,toolbar,android-toolbar,Android,Toolbar,Android Toolbar,我需要在工具栏中插入一种徽标: 这是代码: toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().s

我需要在工具栏中插入一种徽标:

这是代码:

 toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle(titolo);
    toolbar.setLogo(*);
*在这里,我需要插入从url的标志。这可能吗

编辑:

这是我的xml:

<android.support.design.widget.CoordinatorLayout 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="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

无法在此工具栏中插入ImageView。 如何在此xml中创建imageview?

您可以使用

您可以使用


工具栏。setLogo
仅接受可绘制。它将不接受URL。因此,虽然有一种变通方法可以将
imageview
插入工具栏,使用Glide/Picasso加载图像,但这并不等同于设置徽标。 为了从URL设置徽标,您必须从URL创建一个可绘制的资源。代码如下

public static Drawable drawableFromUrl(String url) throws IOException {
Bitmap x;

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();

x = BitmapFactory.decodeStream(input);
return new BitmapDrawable(x); 
}
然后您可以将此抽绳插入徽标。代码如下:

Drawable thisDrawable = drawableFromUrl(url);
toolbar.setLogo(thisDrawable);

这应该符合你的目标。让我知道进展如何。。祝你一切顺利

toolbar.setLogo
仅接受可绘制。它将不接受URL。因此,虽然有一种变通方法可以将
imageview
插入工具栏,使用Glide/Picasso加载图像,但这并不等同于设置徽标。 为了从URL设置徽标,您必须从URL创建一个可绘制的资源。代码如下

public static Drawable drawableFromUrl(String url) throws IOException {
Bitmap x;

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();

x = BitmapFactory.decodeStream(input);
return new BitmapDrawable(x); 
}
然后您可以将此抽绳插入徽标。代码如下:

Drawable thisDrawable = drawableFromUrl(url);
toolbar.setLogo(thisDrawable);

这应该符合你的目标。让我知道进展如何。。祝你一切顺利

接下来是工具栏。setLogo(myImageView)?@francescobocci上面的代码只是一个例子,您必须在Glide的“加载”方法中传递徽标url才能从url加载图像。Glide将下载图像,然后将其设置为MyImageView查看我的编辑。。当我在xml中插入“imageview”时,我在androidstudio预览中看不到他。。事实上,当我使用你的代码时,他们不会这样做nothing@francescobocci请给我一些时间,让我用适当的方式让代码为您工作code@francescobocci我已经编辑了我的代码,还附上了一个截图。我已从drawable设置了此图像。但是,正如我在回答中提到的,当你想要加载图片表单url时,你可以使用Glide。这将从ImageView中的url加载图像,接下来是
工具栏。setLogo(myImageView)
?@francescobocci上述代码只是一个示例,您必须在Glide的“加载”方法中传递徽标url才能从url加载图像。Glide将下载图像,然后将其设置为MyImageView查看我的编辑。。当我在xml中插入“imageview”时,我在androidstudio预览中看不到他。。事实上,当我使用你的代码时,他们不会这样做nothing@francescobocci请给我一些时间,让我用适当的方式让代码为您工作code@francescobocci我已经编辑了我的代码,还附上了一个截图。我已从drawable设置了此图像。但是,正如我在回答中提到的,当你想要加载图片表单url时,你可以使用Glide。这将从imageview中的url加载图像