c# - 在 Xamarin 主项目的 Android 工具栏中显示/隐藏 ImageView
问题描述
这是我的情况。我有一个适用于 iOS 和 Android 的 Xamarin 应用程序,其中一个要求是第一个“主”页面需要显示公司徽标并以工具栏为中心。所有其他页面都将显示页面标题而不是徽标。我必须toolbar.axml
在 Android 项目中自定义文件以使其使用 an 正确显示,ImageView
但它现在出现在每个页面上。我想知道的是,是否可以从共享的 Xamarin 项目(如文件或文件)中以编程方式访问ImageView
并将其可见性属性设置为可见?或者,如果做不到这一点,是否可以从 android 项目中执行此操作,例如在文件中?App.xaml.cs
MainPage.xaml.cs
MainActivity.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 项目,所以我在这里进行的过程中有点想明白这一切。
解决方案
您可以使用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 及更高版本
推荐阅读
- java - 通过直接更改字节来更改音频流的音量
- powershell - 希望通过另一台机器/自定义脚本执行将 Azure 文件共享映射为 Azure Windows VM 上的映射网络驱动器
- python - keras 在 vgg16 特征上训练 LSTM 模型
- php - 用mysql在php中显示排序数据
- image - 如何使用 ImageMagick 调整图像大小?(苹果系统)
- ruby - Kqueue dirextended 看什么动作?
- android - 如何编写索引?
- c++ - CMake 声明函数对 ExternalProject_Add 的依赖
- sql - postgres SQL查询中如何实现数学表达式?
- kubernetes - Openshift:pod 之间的可见性