简体   繁体   English

如何使用Android XML Layout?

[英]How to use Android XML Layout?

The purpose of the XML layout is confusing for me, having only worked with UI in Visual Studio with the drag-drop xml layout and C# code-behind. XML布局的目的令我感到困惑,因为它只能在Visual Studio中通过拖放式xml布局和C#代码在后台使用UI。 Should I be creating a separate xml layout for every screen (load, main, data) in my application? 我应该为我的应用程序中的每个屏幕(负载,主屏幕,数据)创建一个单独的xml布局吗? How do views associate with layouts? 视图如何与布局关联? How do I reference the xml and use it in my code? 如何引用xml并在代码中使用它? I only know of the setContentView(xml layout) call. 我只知道setContentView(xml layout)调用。

For example, for my loading screen I want to display a background image and a loading bar. 例如,对于我的加载屏幕,我想显示背景图像和加载栏。 I'm thinking there's a way for the layout to contain the picture and the widget so the user sees the loading screen immediately, and in the actual code I'm initializing all my necessary variables. 我在想有一种布局可以包含图片和小部件,以便用户立即看到加载屏幕,并且在实际代码中,我正在初始化所有必需的变量。

If you are confused on how layout works in Android , better try reading this topic on XML Layouts and also read this tutorial about user interface in Android for a head start. 如果您对Android中布局的工作方式感到困惑,最好尝试阅读XML Layouts上的本主题,并阅读有关Android用户界面的本教程获取领先。

Here's also a tutorial on how to set background image to progress bar . 这也是有关如何将背景图像设置为进度条的教程。

You should load the layout resource( main.xml ) from your application code, in onCreate method of your Activity . 您应该在Activity onCreate方法中从应用程序代码加载布局资源( main.xml )。

setContentView(R.layout.main);

Yes, you need an XML file for every page of your application. 是的,您需要为应用程序的每个页面提供一个XML文件。 Write a XML file for that particular page mentioning the fields required and in your .java file use this command 为该特定页面编写一个XML文件,其中提及必填字段,并在您的.java文件中使用此命令

setContentView(R.layout.main);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM