学习Android UI

XML in Android: Basics And Different XML Files Used In Android

XML stands for Extensible Markup Language. XML is a markup language much like HTML used to describe data.  XML tags are not predefined in XML. We must define our own Tags. Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. In Android we use xml for designing our layouts because xml is lightweight language so it doesn’t make our layout heavy.

In this article we will go through the basic concepts of xml in Android and different XML files used for different purpose in Android. This will help you in writing a UI code to design your desired user interface.


Basics Of User Interface:

The whole concept of Android User Interface is defined using the hierarchy of View and ViewGroup objects. A ViewGroup is an invisible container that organizes child views. These child views are other widgets which are used to make the different parts of UI. One ViewGroup can have another ViewGroup as an child element as shown in the figure given below:

基本UI用户界面视图组和视图关系图

在上图视图组中(线性布局)包含一个视图组(即。 相对布局图)和两个视图(按钮文本框). 另外两个视图(即2 编辑文本 )都嵌套在里面 相对布局图 视图组。需要注意的是,一个布局可以嵌套在另一个布局中。

下面的代码片段将以更好的方式解释上面的图像。粘贴到 activity_main.xml:


<线性布局xmlns:android=”http://schemas.android.com/apk/res/android"
 android:orientation=“vertical”android:layout\u width=“match\u parent”
 android:layout\u height=“match\u parent”>


 <按钮
 android:id=“@+id/buton1”
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text=“按钮”/>

 
</线性布局>

Android中的XML UI及示例

每个Android应用程序屏幕都有一些组件,如 按钮,文本或图像。它们包含在视图组中。布局是视图组的最佳示例。android中的不同布局类型包括 线性布局相对布局图绝对布局, 表格布局 和 框架布局图.


Android中使用的不同XML文件:

在Android中,有几个xml文件用于不同的用途。下面我们定义每一个。

1、布局XML文件: 布局xml文件用于定义应用程序的实际UI(用户界面)。它包含我们希望在应用程序中使用的所有元素(视图)或工具。就像 文本框的, 按钮的和其他UI元素。

Location in Android Studio:

您将在 物件 文件夹中还有一个名为 布局 您将从中获得各自活动或片段的所有布局文件。

在Android Studio中布局XML

Basic Layout XML Code:

下面我们展示 activity_main.xml file in which we have two 文本框’s.









2. Manifest xml File(Mainfest.xml): This xml is used to define all the components of our application. It includes the names of our application packages, our Activities, receivers, services  and the permissions that our application needs. For Example – Suppose we need to use internet in our app then we need to define Internet permission in this file.

Location in Android Studio:

It is located inside app > manifests folder

Android Studio中的AndroidManifest xml位置

Defining Internet Permission in AndroidManifest.xml

Below we show the AndroidManifest.xml file and define the Internet Permission in that file.


     







3、字符串xml文件(Strings.xml): 此xml文件用于将硬编码字符串替换为单个字符串。我们在这个xml文件中定义所有字符串,然后从这个文件在我们的应用程序(活动或布局xml文件)中访问它们。该文件增强了代码的可重用性。

Location in Android Studio:

字符串xml位置android studio

下面我们展示 串。xml 文件并在文件中定义字符串。


我的应用程序

你好,世界</字符串>
设置
用户登录
<!-- 在此处定义字符串-->

4、样式xml文件(Styles.xml): 此xml用于定义不同的样式并查找应用程序的UI(用户界面)。我们在此文件中定义自定义主题和样式。

Location in Android Studio:

样式位置xml Android Studio

下面我们展示了这种风格。xml文件。



<!-- 基本应用程序主题。-->
<style name=“AppTheme”parent=“Theme.AppCompat.Light.DarkActionBar”>
<!-- 在此处自定义主题。-->
</风格>


5、可绘制xml文件: 这些xml文件用于为应用程序的元素或视图提供各种图形。当我们需要创建自定义UI时,我们使用可绘制的xml文件。假设我们需要在 按钮 或者视图的任何自定义形状,然后我们创建一个可绘制的xml文件,并将其设置在视图的背景中。

请阅读: 如何在Android Studio中创建可绘制的资源XML文件

下面我们展示 custom\u可绘制。xml 文件并使用“样式”属性创建渐变背景色。


<形状xmlns:android=”http://schemas.android.com/apk/res/android">
<!-- 定义渐变的开始、中心和结束颜色-->
<梯度
android:centerColor=“#0f0”
android:endColor=“#00f”
android:startColor=“#f00”/>
</形状>

6、颜色xml文件(colors.xml): This file is used to define the color codes that we used in our app. We simply define the color’s in this file and used them in our app from this file.

Location in Android Studio

颜色xml位置Android Studio

Below we show the colors.xml file in which we define green and white color.




#0f0
#fff

7. Dimension xml File(dimens.xml): This xml file is used to define the dimensions of the View’s. Suppose we need a Button with 50dp(density pixel) height then we define the value 50dp in dimens.xml file and then use it in our app from this file.

Location in Android Studio:

Android Studio中的维度xml文件

Below we show the dimens.xml file in which we define 50dp  dimension for Button height.



16dp
16dp50dp

下载此 免费的 电子书!

这本免费电子书将帮助您在Android Studio中掌握Android应用程序开发的学习!

21 thoughts on “XML in Android: Basics And Different XML Files Used In Android”

  1. When using AccountManager an xml file is needed to create an account (in settings). This is not, apparently, a resource xml file but I can find little documentation on how to use this xml file

  2. Hi Abhishek Saini Vaiya,

    I hope that you are doing well. I am from Bangladesh. My name is Jahangir Alom Minto. I am learning Android App Development. I always follow your abhiandroid.com site. This is just a great resource and your Master Android Development ebook also a great material for android learners. 

    Thank you so much for your great mind and free contribution to Android Learning.
    Best Regards

    Jahangir Alom Minto

  3. Thank you very much for these details. It is very clear and easy to understand. It helps me a lot in my presentation. Thanks again!
    Keep it up!!!!

  4. best one Abhi Sir,

    You have been cracked down the mountain with the simple page….

    Good job sir,,
    may god Reward you with the right thing sir….

    Thanks
    Riyaz Abdul Razak

Leave a Reply

Your email address will not be published. Required fields are marked *



Also Read:

免费下载-主Android应用程序开发侧栏

下载此 免费的 电子书!

这本免费电子书将帮助您在Android Studio中掌握Android应用程序开发的学习!
关闭链接

Android开发者Facebook群免费

下载此 免费的 电子书!

这本免费电子书将帮助您在Android Studio中掌握Android应用程序开发的学习!
关闭链接

下载此 免费的 电子书!

这本免费电子书将帮助您在Android Studio中掌握Android应用程序开发的学习!
关闭链接

下载此 免费的 电子书!

这本免费电子书将帮助您在Android Studio中掌握Android应用程序开发的学习!
关闭链接

看看AbhiAndroid 一步一步地 视频培训帮助您掌握Android应用程序开发

视频培训- 定期添加新内容,逐步解锁视频培训。开发Android应用程序。
Android应用程序源代码- 通过文档获取令人惊叹的电子商务、食品订购和终极WebView源代码。
立即获取访问权限
关闭链接