Android动态设置控件宽高和padding
生活随笔
收集整理的這篇文章主要介紹了
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"><TextViewandroid: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() {@Overridepublic 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);} });效果如下:
?
總結
以上是生活随笔為你收集整理的Android动态设置控件宽高和padding的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios系统越狱才可以享受的100项操作
- 下一篇: android sina oauth2.