首页 > 解决方案 > 在 Xamarin 主项目的 Android 工具栏中显示/隐藏 ImageView

问题描述

这是我的情况。我有一个适用于 iOS 和 Android 的 Xamarin 应用程序,其中一个要求是第一个“主”页面需要显示公司徽标并以工具栏为中心。所有其他页面都将显示页面标题而不是徽标。我必须toolbar.axml在 Android 项目中自定义文件以使其使用 an 正确显示,ImageView但它现在出现在每个页面上。我想知道的是,是否可以从共享的 Xamarin 项目(如文件或文件)中以编程方式访问ImageView并将其可见性属性设置为可见?或者,如果做不到这一点,是否可以从 android 项目中执行此操作,例如在文件中?App.xaml.csMainPage.xaml.csMainActivity.cs

这是我的工具栏的样子。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:minHeight="?android:attr/actionBarSize">

<ImageView
    android:id="@+id/homeLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo_file"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:paddingRight="50dp"
    android:scaleType="fitCenter"
    android:contentDescription="@string/app_name"
    android:visibility="gone"/>
</android.support.v7.widget.Toolbar>

我只需要能够将homeLogo主页的可见性设置为可见。这是我第一次真正深入研究一个真正的 Xamarin 项目,所以我在这里进行的过程中有点想明白这一切。

标签: c#androidxamarinxamarin.formsxamarin.android

解决方案


您可以使用NavigationPage.TitleView然后自定义您的样式,

例如,您想让homeLogo页面有一个带有 ImageView 的工具栏,您可以在以下位置执行此操作homeLogo.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App11"
         x:Class="App11.MainPage">

  //custom your style
  <NavigationPage.TitleView>
    <StackLayout > 
        <Image Source="ic_action_info.png" /> 
    </StackLayout>
  </NavigationPage.TitleView>


</ContentPage>

PS:需要 Xamarin.Forms 3.2.0 及更高版本


推荐阅读