首页 > 解决方案 > 为 BrowseSupportFragment 膨胀新布局

问题描述

我想在我的 Android 电视应用程序上为我的 BrowseSupportFragment 增加新的自定义布局。我尝试了很多方法,但都没有成功。BrowseSupportFragment 的根布局是 lb_browse_fragment 我尝试使用相同的 Id 创建新文件并按如下方式对其进行膨胀,但出现错误。有人可以帮我处理这个案子吗?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/browse_dummy"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- BrowseFrameLayout serves as root of transition and manages switch between
     left and right-->
<androidx.leanback.widget.BrowseFrameLayout
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:descendantFocusability="afterDescendants"
    android:id="@+id/browse_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.leanback.widget.BrowseRowsFrameLayout
        android:id="@+id/browse_container_dock"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.leanback.widget.ScaleFrameLayout
            android:id="@+id/scale_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.leanback.widget.BrowseRowsFrameLayout>
    <!-- Padding needed for shadow -->
    <FrameLayout
        android:id="@+id/browse_headers_dock"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingEnd="50dp" />
</androidx.leanback.widget.BrowseFrameLayout>

我的 MainFragment 扩展了 BrowseSupportFragment :

@Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
   savedInstanceState) {
    View view = inflater.inflate(R.layout.browse_fragment_costumized, container, false);
    return view;  
   }

标签: android-tvleanback

解决方案


BrowseSupportFragment在 onCreateView 中做了很多设置,包括确保处理过扫描并设置了很多引用。如果不调用 onCreateView,您将无法以有意义的方式使用此 Fragment,否则您将打破它在内部所做的所有假设。

如果您要大幅更改布局,则不应使用 BrowseSupportFragment。如果你只是需要改变一些小东西,你可以调用 super onCreateView 方法,然后修改返回的 View。


推荐阅读