【问题标题】:How and Where to define this ImageView in xml?如何以及在何处在 xml 中定义此 ImageView?
【发布时间】:2019-07-17 20:23:52
【问题描述】:

在这个Android Navigation Icon - Profile picture instead of hamburger iconstackoverfolw 帖子中

ImageView profileImage = (ImageView) binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);

如何在哪里定义R.id.iv_profile_image在哪个xml文件中?

【问题讨论】:

    标签: java android navigation-drawer picasso


    【解决方案1】:

    我在您链接的示例中找不到 XML 代码。但是,我注意到 Activity 的 onCreate() 方法中有以下行。这表明正在使用的 XML 文件称为 activity_maps.xml。这很可能是放置 ImageView 的位置。

    binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);
    

    假设这个假设是正确的,ImageView 可以这样声明。 XML 中“在哪里”的答案取决于 XML 是一种声明性语言。

    <ImageView  android:id="@+id/iv_profile_image"
                android:layout_width="60dp"
                android:layout_height="60dp"
                ...
                android:contentDescription="@string/to_replace" />
    

    【讨论】:

    • 好的。那么ActivityMapsBinding binding; 是什么?是标准班吗?我找不到它。
    • 我不认为这是一个标准课程,因为我找不到任何关于它的文档。但似乎您关心的是正确地将 ImageView 添加到工具栏?这可以通过以下方式完成,((Toolbar) getSupportActionBar()).setNavigationIcon(YOUR_ICON); 其中“YOUR_ICON”可以是可绘制对象。在这种情况下,您不需要使用 ActivityMapsBinding。
    猜你喜欢
    • 2017-03-02
    • 2012-06-22
    • 1970-01-01
    • 2018-03-11
    • 2021-10-21
    • 1970-01-01
    • 2020-05-28
    • 2013-06-24
    • 2020-11-13
    相关资源
    最近更新 更多