How can I set the value for the attribute layout_weight
for button in android dynamically from java code ?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Use
LinearLayout.LayoutParams
:EDIT: Ah, Erich's answer is easier!
If you have already defined your view in layout(xml) file and only want to change the weight pro grammatically, then then creating new LayoutParams overwrites other params defined in you xml file.
So first you should use "getLayoutParams" and then setLayoutParams
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams(); params.weight = 4f; mButton.setLayoutParams(params);
Any of
LinearLayout.LayoutParams
andTableLayout.LayoutParams
worked for me, for buttons the right one isTableRow.LayoutParams
. That is:About using
MATCH_PARENT
orWRAP_CONTENT
be the same.Using Kotlin you can do something like:
If you already define your view in your layout(xml) file, only want to change the weight programmatically, this way is better
new a LayoutParams overwrites other params defined in you xml file like margins, or you need to specify all of them in LayoutParams.
You can pass it in as part of the
LinearLayout.LayoutParams
constructor:The last parameter is the weight.