Android动态设置控件宽高和padding

 

在开发过程中我们经常需要动态设置控件的宽高和padding。以TextView为例。如下:

布局代码

TextView的默认宽高为包裹文字,添加了一个背景颜色设置参数后更直观看到效果

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f0f"
        android:text="Hello World!" />

</android.support.constraint.ConstraintLayout>

 

Activity代码

方法一:这种方法能实现在代码中动态设置控件的宽高,而多数情况下都是根据父控件或者同级控件来动态设置控件的宽高这时候就需要动态获取父控件或者同级控件的宽高了。

root = findViewById(R.id.root);
tv = findViewById(R.id.tv);

//获取控件的布局参数,设置控件的宽高
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.width = 200;
params.height = 200;
tv.setLayoutParams(params);
tv.setGravity(Gravity.CENTER);
tv.setPadding(50, 50, 50, 50);

以获取父控件宽高为例。问题来了,不管在activity生命周期的onCreate()、onStart()或者onPause()中都无法获取父控件的宽高。那如何才能获取到父控件的参数呢?如下

调用getViewTreeObserver方法,该方法返回当前控件布局树的观察者对象。

源码
/**
 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
 * observer can be used to get notifications when global events, like
 * layout, happen.
 *
 * The returned ViewTreeObserver observer is not guaranteed to remain
 * valid for the lifetime of this View. If the caller of this method keeps
 * a long-lived reference to ViewTreeObserver, it should always check for
 * the return value of {@link ViewTreeObserver#isAlive()}.
 *
 * @return The ViewTreeObserver for this view's hierarchy.
 */
public ViewTreeObserver getViewTreeObserver() {
    if (mAttachInfo != null) {
        return mAttachInfo.mTreeObserver;
    }
    if (mFloatingTreeObserver == null) {
        mFloatingTreeObserver = new ViewTreeObserver(mContext);
    }
    return mFloatingTreeObserver;
}

注册全局回调

/**
 * Register a callback to be invoked when the global layout state or the visibility of views
 * within the view tree changes
 *
 * @param listener The callback to add
 *
 * @throws IllegalStateException If {@link #isAlive()} returns false
 */
public void addOnGlobalLayoutListener(OnGlobalLayoutListener listener) {
    checkIsAlive();

    if (mOnGlobalLayoutListeners == null) {
        mOnGlobalLayoutListeners = new CopyOnWriteArray<OnGlobalLayoutListener>();
    }

    mOnGlobalLayoutListeners.add(listener);
}

在回调中获取当前控件的宽高,再动态设置TextView的参数

//获取控件的布局树,注册可见时的回调
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        mHeight = root.getHeight();
        mWidth = root.getWidth();

        ViewGroup.LayoutParams params = tv.getLayoutParams();
        params.width = mWidth / 4;
        params.height = mHeight / 4;
        tv.setLayoutParams(params);
        tv.setGravity(Gravity.CENTER);

        root.setPadding(50, 50, 50, 50);
    }
});

效果如下:

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值