How can I set LayoutParams
to Fragment
programmatically?
Actually : I wanna add two Fragments
to a LinearLayout
programmatically and I need set android:layout_weight
for them. I am newbie in Fragment
. I don't know whether it is a good way or not to add two Fragments
to a Layout
Sorry. My English is not really well.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
It is the add() method on the FragmentTransaction which adds the fragment to the activity. The add() method takes two parameters. The first parameter is the id of the parent ViewGroup into which the fragment's View is to be inserted. The second parameter is an instance of the fragment to add.
To load a fragment in an activity at first we need to have an activity and xml file which should have a parent layout that may hold the fragment when it is loaded in that layout. For loading a fragment we need to do a fragment transaction that load the fragment in stack.
In android (and most other technologies), views can have subviews, aka "children". Most views can have children and can be a child of a parent view. It's kind of like a "tree". The most obvious feature of being a child of some other view is that the child moves with the parent view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);
params.weight = 3.0f;
fragment.getView().setLayoutParams(params);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With