簡體   English   中英

如何在Android中從XML擴展視圖?

[英]How to Inflate view from XML in Android?

我正在創建一個tableLayout [以XML提供]

添加表Row [以XML創建並以Java誇大]

還向表Row添加了2個textview [以XML創建並以JAVA誇大]

我只能獲取背景和文本顏色,而無法獲取諸如表格的寬度,高度和邊距之類的布局屬性。

  1. 首先聲明您的充氣機。

     LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE); 
  2. 標識並擴大要在當前視圖上投影的新視圖。

     View view = inflater.inflate(R.layout.new_layout,null); 
  3. 您可能希望將新的展開視圖添加到布局中。

     main.addView(view); 

您可以在此處引用其他信息: http : //developer.android.com/reference/android/view/LayoutInflater.html

2019年5月更新(Kotlin):這是您如何在Kotlin中從XML擴展視圖。 這是指一項活動。

val view = this.layoutInflater.inflate(R.layout.dialog_upgrade, null)
mainLayout.addView(view)
LayoutInflater li = LayoutInflater.from(getApplicationContext());
View cv = li.inflate(R.layout.your_layout, null);

mainlayout.addView(cv);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService      (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_layout,null);
mainlayout.addView(view;

請按照上面的步驟充實視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM