问题
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>
回答1:
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.
来源:https://stackoverflow.com/questions/43295981/how-to-add-logo-image-inside-toolbar