2017-01-21 221 views
1

我想更改標題顏色(Hello Android TV!)和行顏色的headeritem(GridItemPresenter)。Android TV,如何更改headeritem的標題和顏色的顏色?

+0

此鏈接可以幫助你:http://stackoverflow.com/questions/33774150/android-tv-changing-text-color-and-font-of-browse-fragment-rows-header – abielita

+0

謝謝,此鏈接幫助。 – KriK

+0

[android-tv改變瀏覽片段行標題的文本顏色和字體]可能的重複(http://stackoverflow.com/questions/33774150/android-tv-changing-text-color-and-font-of-browse- fragment-rows-header) – abielita

回答

1

要更改列表行標題樣式,您必須定製ListRowPresenter使用:

public class CustomListRowPresenter extends ListRowPresenter { 
    public CustomListRowPresenter() { 
     super(); 
     setHeaderPresenter(new CustomRowHeaderPresenter()); 
    } 
} 

public class CustomRowHeaderPresenter extends RowHeaderPresenter { 
    @Override 
    public Presenter.ViewHolder onCreateViewHolder(ViewGroup parent) { 
     Presenter.ViewHolder viewHolder = super.onCreateViewHolder(parent); 
     RowHeaderView rowHeaderView = (RowHeaderView) viewHolder.view; 
     rowHeaderView.setTypeface(...); 
     rowHeaderView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, ...); 
     return viewHolder; 
    } 
} 

還沒有與任何BrowseFragment直接經驗,但我希望有某種自定義演示者太。

+0

獲得(RowHeaderView)viewHolder.view的ClassCastException。無法將RelativeLayout轉換爲RowHeaderView。我錯過了什麼?我對Leanback比較陌生:P – Krrishnaaaa

+0

@Krrishnaaaa我想,內部的View層次結構可以從Leanback版本變爲版本。我想你需要探索View樹並找到RowHeaderView的位置(如果它已經存在)。 – ULazdins

+0

謝謝..但我想知道是否有任何選項可以將我的自定義視圖傳遞給此標頭?如果有的話,那將是更優化的解決方案。我正在尋找這個解決方案。 – Krrishnaaaa

1
public class CustomListRowPresenter extends ListRowPresenter { 
    public CustomListRowPresenter() { 
     super(); 
     setHeaderPresenter(new CustomRowHeaderPresenter()); 
    } 
} 

class CustomRowHeaderPresenter extends RowHeaderPresenter { 

    @Override 
    public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 
     HeaderItem headerItem = item == null ? null : ((Row) item).getHeaderItem(); 
     RowHeaderPresenter.ViewHolder vh = (RowHeaderPresenter.ViewHolder) viewHolder; 
     TextView title = vh.view.findViewById(R.id.row_header); 
     if(!TextUtils.isEmpty(headerItem.getName())) { 
      title.setText(headerItem.getName()); 
      title.setTextColor(ContextCompat.getColor(FiosTVApplication.getAppContext(), 
        R.color.white)); 
      title.setTypeface(ResourcesCompat.getFont(title.getContext(), R.font.nhaasgroteskdsstd_bold)); 
      title.setTextSize(TypedValue.COMPLEX_UNIT_SP,16); 
     } 
    } 
}