Android TV ImageCardView标题换行

3
我是为 Android TV 开发应用程序并使用 Leanback 库。在谷歌的示例中,他们使用 public class CardPresenter extends Presenter 来显示内容。问题是,我希望在标题中显示所有文本,即不剪切它。

enter image description here

我已经尝试过:

1)通过编程方式设置LayoutParams来解决这个问题:cardView.findViewById(R.id.title_text).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

2)我查看了lb_image_card_view.xml文件中的ImageCard。有趣的是,具有id“title_text”的TextView已经具有android:layout_height =“wrap_content”参数,但看起来它不起作用? 这是一个错误吗?

3)备用计划是为ImageCardView创建自己的类,但这种解决方案似乎太难了。

谢谢。

更新。

我使用了下面的答案,但我必须修改代码以获得更好的性能:

cardView.setOnFocusChangeListener((view, isFocused) -> {
        if (isFocused) {
            ((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(5);
        }
        else {
            ((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(1);
        }
    });
1个回答

5

如果可以的话,尝试查看这个教程。它可以帮助你展示或显示标题中的所有文本。

以下是用于此教程的代码:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    final ImageCardView cardView = new ImageCardView(mContext);    

    cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, final boolean isFocused) {
            final View infoField = view.findViewById(R.id.info_field);
            final TextView contentField = (TextView)view.findViewById(R.id.content_text);
            final TextView titleField = (TextView)view.findViewById(R.id.title_text);
            final Drawable mainImage = ((ImageView)view.findViewById(R.id.main_image)).getDrawable();

            if (isFocused) {
                ((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(3);
                FrameLayout.LayoutParams infoLayout = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                infoField.setLayoutParams(infoLayout);
                RelativeLayout.LayoutParams contentLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                contentLayout.addRule(RelativeLayout.BELOW, R.id.title_text);
                contentField.setLayoutParams(contentLayout);
            }
            else {
                ((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(1);
            }
        }
    });

    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background));
    return new ViewHolder(cardView);
}

这是代码的输出结果。 enter image description here 如需更多信息,请参考此主题

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接