在Android studio新建TV项目的demo上做修改,实现一下需求:
1、去掉顶部背景区域
2、修改中间详情区域高度
3、修改整体背景界面
效果如图:
搜遍全网,没有找到一个解决方案。只能考自己看代码来自定义实现了。
1、去掉顶部背景区域:
VideoDetailsFragment中重写setupDetailsOverviewRowPresenter方法,去掉其中一个alignDef:
@Override
protected void setupDetailsOverviewRowPresenter(FullWidthDetailsOverviewRowPresenter presenter) {
ItemAlignmentFacet facet = new ItemAlignmentFacet();
// by default align details_frame to half window height
ItemAlignmentFacet.ItemAlignmentDef alignDef = new ItemAlignmentFacet.ItemAlignmentDef();
alignDef.setItemAlignmentViewId(androidx.leanback.R.id.details_frame);
alignDef.setItemAlignmentOffset(0);
alignDef.setItemAlignmentOffsetPercent(0);
ItemAlignmentFacet.ItemAlignmentDef[] defs =
new ItemAlignmentFacet.ItemAlignmentDef[] {alignDef};
facet.setAlignmentDefs(defs);
presenter.setFacet(ItemAlignmentFacet.class, facet);
}
完成之后看效果,会发现左上角会有个不好看的返回按钮,点击了没有反应。我们不需要这个返回图标,我们主动调用showTitle方法会发现没有效果,这边我重写方法,领其无论其他类怎么赋值给他,我都不显示标题栏,也就没有返回按钮图标了:
@Override
public void showTitle(boolean show) {
super.showTitle(false);
}
2、修改中间详情区域高度:
自定义一个FullWidthDetailsOverviewRowPresenter类,在里面修改高度:
public class MyDetailsOverviewRowPresenter extends FullWidthDetailsOverviewRowPresenter {
public MyDetailsOverviewRowPresenter(Presenter detailsPresenter) {
super(detailsPresenter);
}
@Override
protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
ViewHolder viewHolder = (ViewHolder) super.createRowViewHolder(parent);
ViewGroup overviewView = viewHolder.getOverviewView();
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) overviewView.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
overviewView.setLayoutParams(layoutParams);
overviewView.setPadding(0,0,0,50); //底部给点边距,不然很难看
return viewHolder;
}
@Override
protected void onLayoutOverviewFrame(ViewHolder viewHolder, int oldState, boolean logoChanged) {
super.onLayoutOverviewFrame(viewHolder, oldState, logoChanged);
//重写这个方法为了解决图片位置偏上的问题
ViewGroup.MarginLayoutParams lpFrame =
(ViewGroup.MarginLayoutParams) viewHolder.getOverviewView().getLayoutParams();
lpFrame.topMargin = 0;
viewHolder.getOverviewView().setLayoutParams(lpFrame);
}
}
然后在VideoDetailsFragment中替换原来的实例化:
private void setupDetailsOverviewRowPresenter() {
// 替换我们的自定义类
FullWidthDetailsOverviewRowPresenter detailsPresenter =
new MyDetailsOverviewRowPresenter(new DetailsDescriptionPresenter());
//设置背景透明
detailsPresenter.setBackgroundColor(ContextCompat.getColor(requireContext(),R.color.transparent));
//设置action栏背景不透明
detailsPresenter.setActionsBackgroundColor(ContextCompat.getColor(requireContext(),R.color.default_background));
//设置详情没有选中效果,不然选中会有个半透白色前景色
detailsPresenter.setSelectEffectEnabled(false);
// Hook up transition element.
FullWidthDetailsOverviewSharedElementHelper sharedElementHelper =
new FullWidthDetailsOverviewSharedElementHelper();
......
3、修改整体背景界面:
原本是通过mDetailsBackground来设置背景的,但是他分开了上边、中间、下边不同的背景,我这边是想要一块完整的背景。
查看VideoDetailsFragment的父类DetailsSupportFragment是有个setBackgroundDrawable方法的,但是没有public,所以我们调用不了。那我们就不用他原来的了,自己找到这个界面的rootView,设置背景即可:
重写onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = super.onCreateView(inflater, container, savedInstanceState);
rootView.findViewById(androidx.leanback.R.id.details_fragment_root)
.setBackground(ContextCompat.getDrawable(requireContext(),R.drawable.image1));
return rootView;
}
去掉原有的背景声明:文章来源:https://www.toymoban.com/news/detail-523373.html
和mDetailsBackground有关的代码都注释掉,就OK了,不然执行了mDetailsBackground.enableParallax()方法则会变成了黑色背景。文章来源地址https://www.toymoban.com/news/detail-523373.html
到了这里,关于Android TV:自定义Leanback的VideoDetailsFragment的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!