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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android LayoutInflater 的使用

發(fā)布時間:2023/11/27 生活经验 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android LayoutInflater 的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

LayoutInflater 有三種實現(xiàn)方法

1?LayoutInflater inflater = LayoutInflater.from(context) 這種方法常用在彈框?

2?LayoutInflater inflater = getLayoutInflater(); 這樣方法自己用在Fragment 以用布局

3?LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

下面已popwindow 為例來說下3中方法實現(xiàn)

第一種

public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;LayoutInflater inflater;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);inflater = LayoutInflater.from(this);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = inflater.inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}

不過一般都不是這樣寫的太麻煩了一般都是直接寫在一起的如下

public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = LayoutInflater.from(InflateActivity.this).inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}

第二種方法實現(xiàn)

public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = getLayoutInflater().inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}

第三種方法實現(xiàn)

public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;LayoutInflater inflater;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);inflater= (LayoutInflater)InflateActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = inflater.inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}

簡單的會使用了在看看inflate 方法

當插入的是一個整體布局,比如彈框之類的

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {return inflate(resource, root, root != null);
}

resource 是布局,布局是一個整體,所以呢root 我們填寫為null

假如現(xiàn)在要一個布局里面插入另外一個布局 類似下面的

public class InflateActivity extends AppCompatActivity {private LinearLayout cons;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);cons = findViewById(R.id.cons);getLayoutInflater().inflate(R.layout.inflate_one_layout,cons,true);
}}

這個時候就是使用到了

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

這個root 就是父布局 ,attachToRoot 是,當講view 添加到父布局中的時候設(shè)置為true ,當不想添加如果已經(jīng)添加過了

設(shè)置為false ,,,那使用fragment 的時候為啥設(shè)置了false ,這個是因為fragment 里面有個什么方法會自動添加?

如果設(shè)置為true ,就是添加了2次 所以會報錯

?

?

總結(jié)

以上是生活随笔為你收集整理的Android LayoutInflater 的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。