简体   繁体   中英

android:layout_alignParentBottom by code

如何为视频设置android:layout_alignParentBottom="true" ,代码而非xml?

I assume you are trying to add your VideoView to a RelativeLayout ?

RelativeLayout relativeLayout = findViewById(R.id.your_relative_layout);
mVideo = new VideoView(this); 
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or wrap_content
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(mVideo , layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
YourVideoView.setLayoutParams(params);

get layout params from your view and add rule

 RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
 lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

Some References are Here1 and Here2

this is what you have to do:

Create a RelativeLayout.LayoutParams object. Use addRule(int) or addRule(int, int) to set the rules. The first method is used to add rules that don't require values. Set the parameters to the view (in this case, to each button).

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,   RelativeLayout.LayoutParams.WRAP_CONTENT);  
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);  
    yourView.setLayoutParams(params);        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM