當前位置:首頁 » 安卓系統 » 安卓控制項在哪裡

安卓控制項在哪裡

發布時間: 2023-11-30 22:13:40

㈠ android怎麼獲取 中的控制項

在實際開發中LayoutInflater這個類還是非常有用的,它的作用類似於findViewById()。不同點是LayoutInflater是用來找res/layout/下的xml布局文件代碼塊,並且實例化;而findViewById()是找xml布局文件下的具體widget控制項(如Button、TextView等)。 具體作用:
1、對於一個沒有被載入或者想要動態載入的界面,都需要使用LayoutInflater.inflate()來載入;
2、對於一個已經載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。
LayoutInflater 是一個抽象類,在文檔中如下聲明:
public abstract class LayoutInflater extends Object
獲得 LayoutInflater 實例的三種方式:
1.LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1. LayoutInflater inflater = LayoutInflater.from(context);
其實,這三種方式本質是相同的,從源碼中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼:
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其實是調用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater ==null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其實調用 context.getSystemService()。
結論:所以這三種方式最終本質是譽基胡都是調慶攔用的Context.getSystemService()。鋒伍
inflate 方法 通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
public View inflate (int resource, ViewGroup root);
3 public View inflate (XmlPullParser parser, ViewGroup root);
4 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot);
5 public View inflate (int resource, ViewGroup root, boolean attachToRoot);
6
7 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
8 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
9 //EditText editText = (EditText)findViewById(R.id.content);
10 // error
EditText editText = (EditText)view.findViewById(R.id.content);
對於上面代碼,指定了第二個參數 ViewGroup root,當然你也可以設置為 null 值。
注意:
·inflate方法與 findViewById 方法不同;
·inflater 是用來找 res/layout下的 xml 布局文件,並且實例化;
·findViewById() 是找具體 xml 布局文件中的具體 widget 控制項(如:Button、TextView 等)。

㈡ android中獲得控制項的位置(相對於布局文件)

正確的代碼如下所示

java">TextViewtv=(TextView)findViewById(R.id.text);
tv.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){
@Override
publicvoidonGlobalLayout(){
//這里執行獲取位置大小操作
intleft=tv.getLeft();
inttop=tv.getTop();
intbottom=tv.getBottom();
intright=tv.getRight();
//tv相對於父布局的坐標就得出來了

}
}

view的位置和大小是通過onLayout和onMeasure方法計算出來的,執行到activity的onCreate()方法時,尚未開始計算控制項的大小和位置,所以是取不到的

OnGlobalLayoutListener 是ViewTreeObserver的內部類,當一個視圖樹的布局發生改變時,可以被ViewTreeObserver監聽到,這是一個注冊監聽視圖樹的觀察者(observer),在視圖樹的全局事件改變時得到通知

除了OnGlobalLayoutListener ,ViewTreeObserver還有如下內部類:


interfaceViewTreeObserver.OnGlobalFocusChangeListener

當在一個視圖樹中的焦點狀態發生改變時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnGlobalLayoutListener

當在一個視圖樹中全局布局發生改變或者視圖樹中的某個視圖的可視狀態發生改變時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnPreDrawListener

當一個視圖樹將要繪制時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnScrollChangedListener

當一個視圖樹中的一些組件發生滾動時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnTouchModeChangeListener

當一個視圖樹的觸摸模式發生改變時,所要調用的回調函數的介面類

㈢ android 一個layout中有很多個子控制項,怎麼去獲得其中的一個控制項

一個layout就是一個容器,你可以放一些子控制項,當你要操作子控制項的時候,你就需要找對應的子控制項,再去操作它(操作比如設置字體,顏色,樣式等)。
可以通過findViewById()方法 來獲取,前提是,你的子控制項需要定義一個id,然後在通過該方法來尋找並操作它。

還可以通過findViewWithTag()方法來找到子控制項(前提是你第一次找到這個子控制項時,並設置一個tag),該方法一般用在activity調用adapter裡面填充布局裡面的子控制項。

熱點內容
消費券腳本 發布:2024-07-27 13:23:13 瀏覽:776
ip電腦伺服器未響應如何處理 發布:2024-07-27 13:22:53 瀏覽:381
m6傳祺2021款買哪個配置 發布:2024-07-27 13:22:04 瀏覽:461
emojijava 發布:2024-07-27 12:57:07 瀏覽:156
編程培訓福州 發布:2024-07-27 12:28:06 瀏覽:876
哈弗h6女生適合哪個配置 發布:2024-07-27 12:10:52 瀏覽:954
memcached啟動腳本 發布:2024-07-27 11:55:41 瀏覽:558
電動車怎麼看配置 發布:2024-07-27 11:55:05 瀏覽:238
mfc打開默認文件夾 發布:2024-07-27 11:41:23 瀏覽:648
電腦找不到伺服器的原因 發布:2024-07-27 11:33:58 瀏覽:864