【问题标题】:Android Layouts gets overlapped when using fragments and inflater to inflate the layout to fragments使用片段和充气器将布局充气到片段时,Android 布局会重叠
【发布时间】:2015-04-16 04:32:26
【问题描述】:

我制作了一个导航抽屉活动,并希望每次从导航抽屉中选择列表项时更改布局。我已使用以下代码使其工作。我得到的唯一问题是当我在布局之间切换时,一切都搞砸了。来回切换 2 个布局会使布局重叠。

这是我用于扩展布局的 Java 代码。

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = null;
        switch(getArguments().getInt(ARG_SECTION_NUMBER)){

            case 1:
                inflater.inflate(R.layout.fragment_introduction, container, true);
                break;

            case 2:
                inflater.inflate(R.layout.fragment_blank, container, true);
                break;

        }
        return rootView;
    }

这是重叠的样子。

【问题讨论】:

  • 设置片段的背景颜色。
  • 你是在使用replace还是add将fragment添加到activity中?
  • 我只是用充气机给布局视图充气。你能给我一个如何替换片段布局的例子吗?对不起,我有点菜鸟。 @Praveen
  • 我的意思是我想查看您的活动代码,您在其中使用片段事务向活动添加片段

标签: android layout navigation-drawer layout-inflater


【解决方案1】:

您将这些视图中的每一个附加到容器中

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView;
    switch(getArguments().getInt(ARG_SECTION_NUMBER)){ 

        case 1: 
            rootView = inflater.inflate(R.layout.fragment_introduction, container, false);
            break; 

        case 2: 
            rootView = inflater.inflate(R.layout.fragment_blank, container, false);
            break; 

    }
    return rootView;
}

我强烈建议为此找到不同的解决方案。也许只是用第二个视图创建另一个片段。保持这种状态看起来将是一场灾难。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多