我可以在片段中膨胀之前修改布局(以编程方式)吗?

时间:2017-02-16 10:49:42

标签: android

我有一个简单的片段:

public class FR_FragmentPopup extends Fragment {

    public FR_FragmentPopup() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        MainActivity.getMain().doFragment(new FR_RecycleViewSettings2(),R.id.over_frame_bottom,false);
        return inflater.inflate(R.layout.fragment_popup, container, false);
    }

}

我想修改xml以包装高度,但是以编程方式,因为我也使用其他片段中的布局。我知道R.layout.fragment_popupint而不是{{1这使得编辑它变得很困难。

view

我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_help, container, false);
        rootView.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
        return rootView;
    }

答案 1 :(得分:1)

理论上:

View view = inflater.inflate(R.layout.fragment_popup, container, false);

view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

return view;

实际上:

没试过。