【问题标题】:Fragment doesn't fill screen properly片段无法正确填满屏幕
【发布时间】:2016-10-13 00:48:32
【问题描述】:

我在另一个 Fragment 中实例化一个 Fragment。内部片段的布局定义为match_parent 的宽度和高度。还有他们的父母。

这是主片段的代码。里面的CustomFragment是内部Fragment。它是带有日历的常规片段。

public class CalendarFragment extends Fragment {
...
public void onViewCreated( LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState ){
customFragment = new CustomFragment();
                    customFragment.setArguments( bundle );

                    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                    transaction.add( R.id.calendar_container, customFragment );
                    transaction.commit();
...
}

当这个片段生效时,包含日历的内部片段会根据每个日历单元格的内容调整其高度。 截屏。注意空白:

如果两个 Fragment 在其布局中都有 match_parent 为什么会发生这种行为?

【问题讨论】:

  • 如果您设置为android:fitsSystemWindows="true",请在您的Activity 中删除它。
  • @Ironman 我在清单中没有这个参数。目前,我的解决方案是根据父片段高度和行数为每个单元格设置最小高度

标签: android android-fragments measurement


【解决方案1】:
  1. 请确保您的第二个片段(如果有)的容器也是match_parent
  2. 尝试为片段赋予不同的背景颜色(即 android:color/red)以检查片段是否实际上已经全屏,但具有透明背景。并且您的日历视图仅位于片段区域的一小部分。

【讨论】:

  • 正如我所说,两者都是 match_parent
【解决方案2】:

尝试替换这一行:

transaction.add( R.id.calendar_container, customFragment );

用这一行:

transaction.replace(R.id.calendar_container, customFragment);

并在提交后添加这一行:

getFragmentManager().executePendingTransactions();

我希望它会帮助你,对我来说它正在做这项工作。

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2012-11-02
    相关资源
    最近更新 更多