我有一个LinearLayout,其中包含其他布局。像这样
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:visibility="gone"
tools:visibility="visible">
<include
layout="@layout/new_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:layout_weight="1"/>
</LinearLayout>
现在我想做的是,以编程方式更改所包含布局(new_layout)的“marginBottom”。我怎样才能做到这一点?
我尝试调用各种LayoutParams并尝试更改页边距,但不确定是否正确。任何帮助,不胜感激。
谢谢
最佳答案
尝试这个。使用您的ID进行线性布局
LinearLayout ll =(LinearLayout) findViewById(R.id.linear_layout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 20);
layoutParams.setMargins(left,top,right,bottom);
您可以在其中传递setMargins中的值
以及是否要在dp中输入值。
public void setMargin(Context con,ViewGroup.LayoutParams params,int dp) {
final float scale = con.getResources().getDisplayMetrics().density;
// convert the DP into pixel
int pixel = (int)(dp * scale + 0.5f);
ViewGroup.MarginLayoutParams s =(ViewGroup.MarginLayoutParams)params;
s.setMargins(pixel,pixel,pixel,pixel);
yourView.setLayoutParams(params);
}
您可以在其中传递布局参数的地方
关于android - 如何以编程方式更改“included”布局的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36904861/