Pro Android with Kotlin


CHAPTER 9: User Interface


Download 5.42 Mb.
Pdf ko'rish
bet166/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   162   163   164   165   166   167   168   169   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

196
CHAPTER 9: User Interface
To get the resolution, you can use the following:
val metrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(metrics)
val density = metrics.density
Programmatic UI Design
Usually UI design happens by declaring UI objects (View objects) and containers (ViewGroup 
objects) inside one or more XML layout files. While this is the suggested way of designing a 
UI and most people probably tell you shouldn’t do anything else, there are reasons to take 
away the layout design from XML and do a programmatical layout instead.

You need more dynamics on a layout. For example, your app adds, 
removes, or moves layout elements by user actions. Or you want to 
create a game with game elements represented by View objects that are 
dynamically moving, appearing, and disappearing.

Your layout is defined on a server. Especially in a corporate environment, 
defining the layout on a server makes sense. Whenever the layout of 
an app changes, you don’t need a new version to be installed on all 
devices. Instead, only a central layout engine needs to be updated.

You define a layout builder that allows to specify layouts in Kotlin in a 
more expressive and concise way compared to XML. Take a look at the 
following, for example, which is valid Kotlin syntax:
LinearLayout(orientation="vertical") {
TextView(id="tv1",
width="match_parent", height="wrap_content")
Button(id="btn1", text="Go",
onClick = { btn1Clicked() })
}

You need special constructs that are not defined in XML. While the 
standard way is to define everything in XML as much as possible and do 
the rest in the code, you might prefer a single-technology solution, and 
in that case you have to do everything from inside the code.
Note that if you abandon the descriptive layout via XML files and use a programmatic layout 
instead, you manually have to take care of different screen sizes, screen densities, and other 
hardware characteristics. While this is always possible, under certain circumstances it could 
be a complicated and error-prone procedure. Certain characteristics such as UI element 
sizes can be much more easily expressed in XML than in the code.
To start with a programmatic UI design, it is the easiest if you define a single container 
inside XML and use it in your code. I say this because layouts have their own idea of how 
and when to place their children, so you might end up in a nightmare of timing, layout, and 
clipping issues if your code has another idea about how and when to place views. A good 
candidate is a FrameLayout, as shown here:



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   162   163   164   165   166   167   168   169   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling