安卓 : Set background color for listfragment

标签 android android-fragments android-listfragment

我正在使用 newView 和 bindView 创建 listfragment 的元素。该列表构建良好。列表的每一行都有一个分隔符。

enter image description here

我猜这些分隔符之间的颜色是默认选择的 android 主题。我尝试在 onCreateView() 中使用此代码将此颜色设置为白色。

getListView().setBackgroundColor(Color.WHITE);

但它提示内容 View 尚未创建。

最佳答案

在调用 onCreateView() 之前,Fragment 不会有其内容 View ... 所以 onCreateView() 中的 getListView() 返回 null 并将 onCreateView() 中的代码移动到 onViewCreated()...

通常你不需要覆盖 ListFragment 中的 onCrateView() 方法,因为它会为你覆盖并实例化 ListView 作为它的内容

下面的代码假定您正在扩展 ListFragment...

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView listView = getListView();
        listView.setDivider(new ColorDrawable(Color.WHITE));
        listView.setDividerHeight(3); // 3 pixels height
    }

关于安卓 : Set background color for listfragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20903150/

相关文章:

java - 从表面 View 录制视频(绑定(bind)视频和相机预览)

android - 没有使用 Microsoft Computer Vision API 返回的描述或标签

android - 以编程方式创建水平 ScrollView 不起作用

安卓 : Using selector to change button background image and color

java - Android 在调用 FragmentTransaction Replace 方法时崩溃?

android - 带有自动滚动 android 的 Viewpager

android - 从另一个 Activity 返回后返回到 MainActivity 的 fragment ?

android - 使用调用 startActivity() 重新启动应用程序但得到 “complete action using”

java - 如何将静态 onScroll 事件值从 CollectionView 返回到调用 ListFragment 或使用类中非静态方法中的值?

android-fragments - android - 对多个 Activity 使用相同的片段