日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android动态设置控件宽高和padding

發布時間:2024/3/13 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。