在主图像上放置 image/badge

Placing an image/badge over the main image

我正在尝试在卡片视图中放置标记或徽章,将其放在卡片中间。问题是,如果我用 setMainimage 放置图像同时显示两个图像,我认为它会起作用的方式是第一个显示,然后是第二个(比第一个小),但是事实并非如此。我尝试的第二种方法是将它与样式一起放置,但我对所有卡片使用相同的样式,因此图像显示在我的所有卡片中。我想要一些建议或其他方法。

有几种不同的方法可以实现这一点。一种简单的方法是在 Presenter 中提供您自己的 XML。在您的 XML 中,您可以随意将元素叠加在一起。检查 this SO post 以了解如何实现它。

@Override
final protected BaseCardView onCreateView(Context context) {
    final BaseCardView cardView = new BaseCardView(context, null, R.style.YourCardStyle) {
        @Override
        public void setSelected(boolean selected) {
            // TODO: Add your functionality you want here! Showing/hiding elements.
            super.setSelected(selected);
        }
    };

    cardView.addView(LayoutInflater.from(context).inflate(R.layout.card_your_custom_view, null));
    // Just some init method to set up your views visibility.
    initCardView(cardView);
    return cardView;
}

然后,只要您在 XML 中正确命名您的视图,一切都会如您所愿。您将在 onBindViewHolder

中将模型绑定到您的视图
public void onBindViewHolder(CardModel cardModel, BaseCardView cardView) {
    if (cardModel == null) {
        return;
    }
    Video video = cardModel.clip;
    Context context = cardView.getContext();

    ImageView imageView = (ImageView) cardView.findViewById(R.id.main_image);
    TextView primaryText = (TextView) cardView.findViewById(R.id.primary_text);
    TextView secondaryText = (TextView) cardView.findViewById(R.id.secondary_text);
    TextView extraText = (TextView) cardView.findViewById(R.id.extra_text);
}

使用上面的代码块,您必须将 R.layout.card_your_custom_view 替换为您的布局文件,其中 View 的 ID 为 main_imageprimary_textsecondary_text 和 'extra_text. As well as replacingR.style.YourCardStyle` 与您自己的卡片样式。 Here 是您可以使用的一些卡片样式示例。

要更好地了解 Presenters,请查看 Leanback Showcase 应用程序中的 this folder

如果可行,请告诉我。