2
A
回答
7
基本上需要在運行時基於XML文件創建(或填充)視圖。例如,如果您需要爲您的ListView項目動態生成視圖,這就是它的全部。
5
LayoutInflater用於使用預定義的XML佈局來操縱Android屏幕。 該類用於將佈局XML文件實例化爲其相應的View對象。 它從不直接使用。相反,使用getLayoutInflater()或getSystemService(String)來檢索已連接到當前上下文的標準LayoutInflater實例。
簡單的程序,用於LayoutInflater- 使這個佈局,您activity_main.xml-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
this is the hidden layout which we will add dynamically,save it as hidden_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hidden_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is the inflated text of hidden layout"/>
<EditText android:id="@+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, this is your name"/>
</LineraLayout>
現在,這是主要的活動 - 代碼
public class MainActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout main = (LinearLayout)findViewById(R.id.main_layout);
View view = getLayoutInflater().inflate(R.layout.hidden_layout, main,false);
main.addView(view);
}
}
音符我們使用「假」屬性,因爲通過這種方式,我們對加載的視圖進行的任何進一步的佈局更改都將生效。如果我們將它設置爲「true」,它將再次返回根對象,這將阻止對加載的對象進行進一步的佈局更改。
相關問題
- 1. 爲什麼我在android中使用layoutinflater?
- 2. 什麼時候在Android的
- 3. 在Android中,什麼時候使用Uri,什麼時候使用路徑,何時在路徑之前添加`file://?
- 4. 在TypeScript中,什麼時候使用「let」,什麼時候使用「const」?
- 5. 什麼時候使用for-each和什麼時候在xslt中使用apply-templates?
- 6. 什麼時候使用__proto__和什麼時候使用原型
- 7. 什麼時候應該使用AWS,什麼時候不使用
- 8. intn_t什麼時候使用它,什麼時候不使用
- 9. 什麼時候使用Ruby和什麼時候使用PHP
- 10. 什麼時候使用ByteString,什麼時候不使用?
- 11. Android:我們什麼時候使用getIntent()?
- 12. 什麼等同於在Android中使用LayoutInflater的findViewById?
- 13. 什麼時候在Django中使用「AbstractBaseUser」?
- 14. 什麼時候在javascript中使用'return'
- 15. 什麼時候在swift中使用respondsToSelector?
- 16. 什麼時候在C#中使用類?
- 17. 什麼時候在gridview中使用dynamicfield?
- 18. 什麼時候在mysql中使用OPTIMIZE
- 19. 什麼時候在.NET中使用GC.Collect()?
- 20. 什麼時候在Java中使用「this」
- 21. 什麼時候在C++中使用「declare」?
- 22. 什麼時候在mongodb中使用BSON?
- 23. 什麼時候在hibernate中使用Criteria.ALIAS_TO_ENTITY_MAP?
- 24. 什麼時候應該使用async/await,什麼時候不用?
- 25. 什麼時候使用sIFR?
- 26. 什麼時候使用MessageDigest.reset()
- 27. 什麼時候使用VK_IMAGE_LAYOUT_GENERAL
- 28. 什麼時候使用SVDRecommender
- 29. JOINS什麼時候使用?
- 30. 什麼時候使用SpringApplicationBuilder?
從文檔「_Instantiates佈局XML文件到其相應的視圖對象._」非常簡單。它將XML轉化爲代碼中的視圖。 – csmckelvey