android 在代码中设置布局居中layout_gravity,layout_margin的方法

本文介绍了如何在代码中通过设置LayoutParams来实现布局居中(layout_gravity)与布局间距(layout_margin)的效果,适用于LinearLayout与FrameLayout。详细解释了在不同GroupView中设置这些属性的具体步骤,并提供了实例代码与错误处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


在代码中设置布局居中,翻看api可以知道view中有setGravity,setPadding,但是没有直接的setLayoutGravity,setMargin等方法。下面将在代码中实现类似布局中layout_gravity,layout_margin的方法。

可以通过设置view里面的 LayoutParams 设置,而这个LayoutParams是根据该view在不同的GroupView而不同的。

1、代码中设置layout_gravity

LinearLayout layoutTop=(LinearLayout) findViewById(R.id.layout_top);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(layoutTop.getLayoutParams()); 
params.gravity = Gravity.CENTER_VERTICAL;
layoutTop.setLayoutParams(params);

    这里的FrameLayout、LinearLayout是说明该view在一个FrameLayout或LinearLayout里面,具体得看自己的布局,这个地方有可能会报错,比如图中这样的错误。错误很明显,这也很好解决,只需要按照提示将属性改为相应的即可。144932_bzyf_244918.png


2、代码中设置layout_margin

ImageView image = (ImageView) findViewById(R.id.img_icon);  
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(image.getLayoutParams());  
lp.setMargins(10, 20, 0, 0);  
image.setLayoutParams(lp);
该方法可以封装为:
public static void setMargins (View view, int left, int top, int right, int bottom) {  
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {  
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();  
        p.setMargins(left, top, right, bottom);  
        view.requestLayout();  
    }  
}








转载于:https://my.oschina.net/u/244918/blog/424766

我说了只要相对分布不要擅自修改:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" android:background="#F5F5F5"> <LinearLayout android:layout_width="396dp" android:layout_height="22dp" android:background="#E0E0E0" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:orientation="horizontal"> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:contentDescription="Signal" android:src="@drawable/ic_signal" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:layout_marginLeft="4dp" android:contentDescription="Battery" android:src="@drawable/ic_battery" /> </LinearLayout> </LinearLayout> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/Deco_App" android:contentDescription="Deco Logo" android:layout_gravity="center" android:layout_marginVertical="16dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Deco" android:textSize="20sp" android:textStyle="bold" android:textColor="#666666" android:layout_gravity="center" android:layout_marginBottom="16dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I accept the Terms of Use and confirm that I have fully read and understood the Privacy Policy." android:textSize="12sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="8dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I confirm to join the User Experience Improvement Program. I understand that I can opt out of the program any time." android:textSize="14sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="16dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Continue" android:textColor="#FFFFFF" android:background="@drawable/rounded_button" android:layout_gravity="center" android:textSize="16sp" android:layout_marginBottom="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Disagree and Quit" android:layout_gravity="center" android:textSize="14sp" android:textColor="#00C4CC" /> </LinearLayout>
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值