Set fragments parameters programatically in android

醉酒当歌 提交于 2019-12-21 22:31:54

问题


How to set parameters like height, width ,margins for fragments programatically. I am adding fragments like

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    MyListFragment myListFragment = new MyListFragment();
    fragmentTransaction.add(1, myListFragment);

    DetailFragment detailFragment = new DetailFragment();
    fragmentTransaction.add(1, detailFragment);

    fragmentTransaction.commit();

Also I am using compatabilty jar like android-support-v4.jar.

Thanks.


回答1:


How to set parameters like height, width ,margins for fragments programatically

Fragments do not have "parameters like height, width ,margins". View and ViewGroup have "parameters like height, width ,margins". So, you either adjust the container into which you are placing the fragment (which, for some strange reason, you have declared as 1 in your examples above), or you adjust the View that your Fragment returns from onCreateView().




回答2:


CommonsWare's additional info that you can adjust the View returned by onCreateView(), got me thinking about another approach: simply get the Fragments view and then adjust the LayoutParams of that.

// use the appropriate LayoutParams type and appropriate size/behavior
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
theFragment().getView().setLayoutParams(params);



回答3:


I think there's a valid case here to add margins programatically on the fragment's view and not on the container it's inflated into.

If you want to have your container layout shared with other views for instance.

In my case I have a full-screen FrameLayout containing a button at the top, and the entire screen is then taken by the inflated fragment. I could nest two FrameLayouts one inside the other, but that's not good for draw performance, the better option would be to inflate the fragment directly into the root FrameLayout, but give its view topMargin to prevent it from hiding the button.

Here's some code:

FragmentManager fm = getSupportFragmentManager();
Fragment fragment = new MyFragment();
fm.beginTransaction().add(R.id.container, fragment, "main").commit();

// wait for the fragment to inflate its view within container
container.addOnLayoutChangeListener(new OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if (fragment.getView() != null) {
            LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
            lp.topMargin = 50; // you need to translate DP to PX here!
            fragment.getView().setLayoutParams(lp);

            container.removeOnLayoutChangeListener(this); // prevent infinite loops
        }
    }
});


来源:https://stackoverflow.com/questions/10683374/set-fragments-parameters-programatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!