Android TV ImageCardView 标题换行

Android TV ImageCardView title wrapping

我正在为 Android TV 开发应用程序并使用 Leanback 库。

在 google 的 example 中,他们使用 public class CardPresenter extends Presenter 来显示内容。问题是,我想在标题中显示所有文本,即不要剪切它。

我已经试过了:

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 创建自己的 class,但这个解决方案似乎很难 2。

谢谢。

更新.

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

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);
        }
    });

尝试查看此 tutorial 是否对您有帮助。它可以帮助您显示或显示标题中的所有文本。

这是本教程使用的代码

@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);
}

这是这段代码的输出。

有关详细信息,请同时查看此 thread