![](/img/trans.png)
[英]how to set layout_margin for a view (not for a viewGroup) programmatically in android
[英]Setting android:layout_margin for a view through code
在我的應用程序中,我正在創建LinearLayout。 當我向布局中添加其他視圖時,它們看起來太近(兩個視圖之間的差異非常窄)。 我想為Layout中添加的視圖設置android:layout_margin
。 它怎么可能?
嘗試這個
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 20, 30, 40);
Button button = new Button(this);
button.setText("some text");
layout.addView(button, params);
如果您嘗試通過xml進行操作,則只需執行此操作
android:layout_marginLeft="10dp" //example
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
無論您想在左邊,右邊,頂部還是底部留有余量
嘗試這個
RelativeLayout.LayoutParams my_params;
my_params= new RelativeLayout.LayoutParams(12,12);
my_params.leftMargin =57;
my_params.topMargin =143;
rl_main.addView(mProgress,my_params);
rl_main是主布局, mprogress是添加的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.