在开发过程中我们经常需要动态设置控件的宽高和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);
}
});
效果如下: