簡體   English   中英

在運行時以編程方式設置片段布局邊距

[英]Set the Fragment Layout Margin Programmatically at run time

我想以編程方式設置片段的布局邊距。 有沒有辦法做到這一點。

正如我嘗試使用MapView這樣。

LayoutParams layoutParams = (RelativeLayout.LayoutParams) mapView.getLayoutParams();
layoutParams.setMargins(0, 0, 0, 0);
mapView.setLayoutParams(layoutParams);

在碎片的情況下可以實現相同的效果。

在片段的onCreateView方法中,您可以使用以下代碼以編程方式設置視圖:

// Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.sample_fragment, container, false);
    LayoutParams layoutParams=new LayoutParams(width, height);
    layoutParams.setMargins(leftMargin, topMargin, 0, 0);
    view.setLayoutParams(layoutParams);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM