Android TV ImageCardView 标题换行

Pan*_*xim 3 java android android-tv

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

以谷歌为例,他们用来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 创建自己的类,但这个解决方案似乎很难。

谢谢。

更新。

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

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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

小智 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);
}
Run Code Online (Sandbox Code Playgroud)

这是这段代码的输出。

在此输入图像描述

欲了解更多信息,也请查看此线程