📅  最后修改于: 2021-01-05 05:37:40             🧑  作者: Mango
在本章中,我们将介绍android屏幕的不同UI组件。本章还介绍了进行更好的UI设计的技巧,并说明了如何设计UI。
android应用程序的典型用户界面由操作栏和应用程序内容区域组成。
这些组件也显示在下图中-
android应用程序的基本单元是活动。 UI在xml文件中定义。在编译期间,将XML中的每个元素编译为等效的Android GUI类,其类具有由方法表示的属性。
活动由视图组成。视图只是出现在屏幕上的小部件。可以是按钮,也可以将一个或多个视图组合到一个GroupView中。 ViewGroup的示例包括布局。
有很多类型的布局。下面列出其中一些-
线性布局又分为水平和垂直布局。这意味着它可以在单列或单行中排列视图。这是包含文本视图的线性布局(垂直)代码。
AbsoluteLayout使您可以指定其子级的确切位置。可以这样声明。
TableLayout将视图分为行和列。可以这样声明。
RelativeLayout使您可以指定子视图之间的相对位置。可以这样声明。
FrameLayout是屏幕上的占位符,可用于显示单个视图。可以这样声明。
除了这些属性外,在所有视图和ViewGroups中还有其他公共属性。它们在下面列出-
Sr.No | View & description |
---|---|
1 |
layout_width Specifies the width of the View or ViewGroup |
2 |
layout_height Specifies the height of the View or ViewGroup |
3 |
layout_marginTop Specifies extra space on the top side of the View or ViewGroup |
4 |
layout_marginBottom Specifies extra space on the bottom side of the View or ViewGroup |
5 |
layout_marginLeft Specifies extra space on the left side of the View or ViewGroup |
6 |
layout_marginRight Specifies extra space on the right side of the View or ViewGroup |
7 |
layout_gravity Specifies how child Views are positioned |
8 |
layout_weight Specifies how much of the extra space in the layout should be allocated to the View |
在Android UI上指定元素的大小时,应记住以下度量单位。
Sr.No | Unit & description |
---|---|
1 |
dp Density-independent pixel. 1 dp is equivalent to one pixel on a 160 dpi screen. |
2 |
sp Scale-independent pixel. This is similar to dp and is recommended for specifying font sizes |
3 |
pt Point. A point is defined to be 1/72 of an inch, based on the physical screen size. |
4 |
px Pixel. Corresponds to actual pixels on the screen |
Sr.No | Density & DPI |
---|---|
1 |
Low density (ldpi) 120 dpi |
2 |
Medium density (mdpi) 160 dpi |
3 |
High density (hdpi) 240 dpi |
4 |
Extra High density (xhdpi) 320 dpi |
以下是一些有关创建有效布局的准则。