如何在工具栏中添加徽标图像 [英] How to add Logo image inside Toolbar
问题描述
您好,Xamarin android 新手.有这个问题:
1) 如何在工具栏中添加图片Logo?
W X H 的图像标志尺寸是多少?
这里是工具栏
<android.support.v7.widget.Toolbarxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/工具栏"android:layout_height="wrap_content"android:minHeight="?attr/actionBarSize"android:background="?attr/colorPrimary"android:layout_width="match_parent"/>
谢谢
---- 更新:已解决
解决方案 如何在工具栏中添加图片Logo?
首先需要正确创建自定义Toolbar,可以按照
<块引用>W X H 的 Image Logo 尺寸是多少?
android:layout_width
和 android:layout_height
决定了 ImageView
的大小,但默认情况下,图像不会自行调整通过 ImageView
.您可以通过设置 android:adjustViewBounds="true"
:
来更改此行为<工具栏 xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/工具栏"android:layout_width="match_parent"android:layout_height="wrap_content"android:minHeight="?android:attr/actionBarSize"android:background="?android:attr/colorPrimary"android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"><图像视图android:layout_width="120dp"android:layout_height="120dp"android:adjustViewBounds="true"android:src="@drawable/house"/></工具栏>
有关Android布局的详细信息,请参阅的布局参数部分Android 布局文档.
Hi new to Xamarin android. Have this question:
1) How to add an image Logo inside the Toolbar?
What is the Image Logo size interms of W X H ?
Here the toolBar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent" />
Thanks
---- Update : solved
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent" />
<ImageView
android:id="@+id/toolbarimage"
android:layout_marginTop="12dp"
android:layout_marginBottom="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/Logo" />
</android.support.v7.widget.Toolbar>
解决方案
How to add an image Logo inside the Toolbar?
First, you need to create a custom Toolbar correctly, you can follow the Replacing the Action Bar to create a custom Toolbar.
Then, you can simply add any elements inside of your Toolbar layout:
Toolbar.axml(house.png
in Resources/drawable
folder):
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/house" />
</Toolbar>
MainActivity.cs:
[Activity(Label = "ToolbarDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetActionBar(toolbar);
ActionBar.Title = "";
}
}
And you will get something like this:
What is the Image Logo size interms of W X H ?
The android:layout_width
and android:layout_height
determine the size of the ImageView
,but by default, the image won't adjust itself by ImageView
. You can change this behavior by setting android:adjustViewBounds="true"
:
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:adjustViewBounds="true"
android:src="@drawable/house" />
</Toolbar>
For details about Android Layout, please refer to Layout Parameters section of Android Layout Document.
这篇关于如何在工具栏中添加徽标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!