Android 更改代码中的ImageButton边距
我有一个用xml定义的带有ImageButton的相对布局。图像按钮有一个属性android:layout_marginBottom=“25dp” 是否可以在运行时更改此属性,我想根据屏幕大小移动按钮。我原以为会有ImageButton.setMarginBottom方法,但似乎没有。另一个与android不一致的地方。试试这个:Android 更改代码中的ImageButton边距,android,android-layout,Android,Android Layout,我有一个用xml定义的带有ImageButton的相对布局。图像按钮有一个属性android:layout_marginBottom=“25dp” 是否可以在运行时更改此属性,我想根据屏幕大小移动按钮。我原以为会有ImageButton.setMarginBottom方法,但似乎没有。另一个与android不一致的地方。试试这个: ImageButton imgBtn = (ImageButton)findViewById(R.id.img_btn); LinearLayout.LayoutPa
ImageButton imgBtn = (ImageButton)findViewById(R.id.img_btn);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imgBtn.getLayoutParams();
params.setMargins(0, 0, 0, 25); //bottom margin is 25 here (change it as u wish)
imgBtn.setLayoutParams(params);
您可以使用setPadding函数设置图像按钮的填充,也可以设置按钮的布局参数setLayoutParamsThanks Rajesh,我现在正试图与布局参数达成一致。这个概念似乎相当复杂,文档也不太清楚。你尝试过下面提供的答案吗?非常接近,即将进行一些小的调整。Kool4u,谢谢,这与我在另一篇文章中发现的非常接近。尽管您的.getLayoutParams()可能会解决我当前遇到的问题。我现在感觉更自信了:)谢谢各位,我想我现在已经把问题解决了。如果从xml开始,则必须使用.getLayoutParams(),否则会在不知道的情况下覆盖设置。