2017-01-24 27 views
1

我正在開發適用於Android TV的應用程序並使用leanback庫。Android TV ImageCardView標題包裝

example從谷歌他們正在使用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創建自己的類,但是這個解決方案似乎很難。

Thx。

更新。

我用了以下的答案,但我不得不modificative代碼以提高性能:

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

回答

2

嘗試查看此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); 
} 

這裏是這段代碼的輸出。

enter image description here

欲瞭解更多信息,請檢查該thread也。