2017-04-08 18 views
1

Hi new to Xamarin android。有這個問題:如何在工具欄內添加Logo圖像

1)如何在工具欄中添加圖片Logo?

什麼是W X H的圖片標誌大小間?

這裏的工具欄

<?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" /> 

感謝

----更新:解決

<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> 

enter image description here

回答

1

如何添加工具欄裏面的形象標誌?

首先,您需要正確創建自定義工具欄,您可以按照Replacing the Action Bar創建自定義工具欄。

然後,您可以簡單地添加工具欄佈局內的任何元素:

Toolbar.axml(house.pngResources/drawable文件夾):

<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 = ""; 
    } 
} 

你會得到這樣的東西: enter image description here

什麼是W X H的圖像標誌大小Interms?

android:layout_widthandroid:layout_height確定ImageView的大小,但默認情況下,圖像將不會被ImageView自行調節。您可以通過設置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> 

有關Android的佈局的詳細信息,請參閱的Android Layout Document佈局參數部分。

+0

看到您的解決方案真的很開心。請幫助我,因爲我在安裝V7 appCompat時遇到問題。在這裏我的步驟,我右鍵單擊該項目下的組件,以獲得V7 AppCompat。起初所有與Xamarin.Android.Support.compat和其他Xamarin.Android.Support DLL導入組件和參考文件夾中的DLL。過了一段時間,爲什麼在所有DLL中都附帶了一個黃色三角警告標誌,並以參考文件夾中的Xamarin.Android.Support開頭?這是什麼意思?當我右鍵單擊參考文件夾並選擇Nuget Manager時,它顯示V7 AppCompat已安裝。發生什麼事? – MilkBottle

+0

某些依賴關係可能未完全安裝。請嘗試卸載所有nuget軟件包並再次通過nuget安裝'xamarin.android.support.v7.appcompat'。 –

+0

首先通過組件獲取支持v7 Appcompat的支持。當點擊參考文件夾時,它將顯示v7 Appcompat:1)已安裝25.1.1 - 卸載btn
2.版本25.1.0 - 更新btn
我需要點擊更新btn才能獲得25.1。0如果沒有,我得到黃色三角警告標誌。 25.1.1有問題嗎?必須使用版本25.1.0?感謝 – MilkBottle