Application of XML in Android mobile application development (combined with java in android studio)

As we all know, XML files and java files exist together in Android development. We can understand java as a method of creating and defining objects, while XML file is a visual interaction with users (i.e. UI interface).
XML includes the following interactive functions: animation video, pictures (transparency, size, position), layout, buttons, text display, theme, menu, etc.
1, anim directory
The xml in the anim directory is mainly used for animation in android, including Frame animation and Tween animation.
1. Frame by frame animation
Frame by frame animation is a process in which a series of pictures are displayed in a certain order, which is very similar to the mechanism of playing movies. It can be understood as GIF, display pictures frame by frame.
code:

<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
android:oneshot="false">  
  <item android:drawable="@drawable/a_01" android:duration="50"/>  
  <item android:drawable="@drawable/a_02" android:duration="50"/>  
  <item android:drawable="@drawable/a_03" android:duration="50"/>  
  <item android:drawable="@drawable/a_04" android:duration="50"/>  
  <item android:drawable="@drawable/a_05" android:duration="50"/>  
  <item android:drawable="@drawable/a_06" android:duration="50"/>  
</animation-list>

Element is required and must be used as the root element, which can contain one or more elements; android:onshot: if it is defined as true, the animation will be executed only once. If it is false, it will loop all the time; Element represents a frame of animation, and android:drawable specifies the picture resources corresponding to this frame of animation; android:druation represents the duration of this frame, an integer, in milliseconds.
2. Make up animation
Patching animation includes effects such as rotation, translation, scaling and transparency.
code:
① Spin

<?xml version="1.0" encoding="utf-8"?>



2. Pan:

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
android:interpolator="@android:anim/accelerate_interpolator">  
    <!--  
    beginning x Axis coordinates  
    stop x Axis coordinates  
    beginning y Axis coordinates  
    stop y Axis coordinate scaling  
    -->  
    <translate  
      android:fromXDelta="0%"  
      android:toXDelta="100%"  
      android:fromYDelta="0%"  
      android:toYDelta="100%"  
      android:duration="2000"/>  
</set>  

③ Zoom

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
android:interpolator="@android:anim/accelerate_interpolator">  
    <!--  
    start x Axis coordinates  
    stop x Axis coordinates  
    beginning y Axis coordinates  
    stop y Axis coordinates  
    x Axis coordinates  
    y Axis coordinates  
    -->  
    <scale  
      android:fromXScale="1.0"  
      android:toXScale="0.0"  
      android:fromYScale="1.0"  
      android:toYScale="0.0"  
      android:pivotX="50%"  
      android:pivotY="50%"  
      android:duration="1000"/>  
</set>  

④ Transparency

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
android:interpolator="@android:anim/accelerate_interpolator">  
    <!-- fromAlpha and toAlpha Is the start transparency and the end transparency -->  
    <alpha  
      android:fromAlpha="1.0"  
      android:toAlpha="0.0"  
      android:startOffset="500"  
      android:duration="500"/>  
</set>  

2, drawable directory
drawable directory is mainly used to define the background and click status of pictures and buttons. It mainly uses shape label and selector label.
1.shape label
Shape mainly defines a shape, and then can be set to a button as the background. The most commonly used button is the fillet button.
code:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle"|"oval"|"line"|"ring"] >

 <!-- fillet -->       
 <corners            
        android:radius="integer"            
        android:topLeftRadius="integer"            
        android:topRightRadius="integer"            
        android:bottomLeftRadius="integer"            
        android:bottomRightRadius="integer" />    
  <!-- Gradual change -->        
  <gradient            
        android:angle="integer"            
        android:centerX="integer"            
        android:centerY="integer"            
        android:centerColor="integer"            
        android:endColor="color"            
        android:gradientRadius="integer"            
        android:startColor="color"            
        android:type=["linear"|"radial"|"sweep"]            
        android:useLevel=["true"|"false"] />        
  <padding            
        android:left="integer"            
        android:top="integer"            
        android:right="integer"            
        android:bottom="integer" />        
  <size            
        android:width="integer"            
        android:height="integer" />        
  <solid            
        android:color="color" />    
  <!-- Stroke  -->     
  <stroke            
        android:width="integer"            
        android:color="color"            
        android:dashWidth="integer"            
        android:dashGap="integer" />    
2. Section defines the background of the button
<?xml version="1.0" encoding="utf-8" ?>     
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <!-- Default background picture-->    
    <item android:drawable="@drawable/a_01" />      
    <!-- Background image without focus -->    
    <item android:state_window_focused="false"     
          android:drawable="@drawable/a_01" />   
    <!-- Background image when focus is obtained and clicked in non touch mode -->    
    <item android:state_focused="true"   
          android:state_pressed="true"     
          android:drawable="@drawable/a_02" />   
    <!-- Background picture when clicking in touch mode-->    
    <item android:state_focused="false"   
          android:state_pressed="true"          
          android:drawable="@drawable/a_03" />    
    <!--Picture background when selected-->    
    <item android:state_selected="true"     
          android:drawable="@drawable/a_04" />     
    <!--Picture background when getting focus-->    
    <item android:state_focused="true"    
          android:drawable="@drawable/a_05" />    
</selector> 

3, layout directory
The layout directory mainly stores the layout files of android, including the five layouts in android: linear layout, FrameLayout, RelativeLayout, absolute layout and TableLayout. I won't explain them in detail here. I'm sure you won't have too much problems when using them.
4, menu directory
The menu directory is mainly used to store menu styles, including menu items in the pop-up box when clicking the menu key at the bottom of the phone and the menu button set in the actionbar at the top.
code:

<?xml version="1.0" encoding="utf-8"?>    
<menu xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:id="@+id/connect"    
          android:orderInCategory="100"    
          android:showAsAction="never"    
          android:icon="@android:drawable/ic_menu_send"    
          android:title="connect" />    
    <item android:id="@+id/disconnect"    
          android:orderInCategory="100"    
          android:showAsAction="never"    
          android:icon="@android:drawable/ic_menu_close_clear_cancel"    
          android:title="to break off" />    
    <item android:id="@+id/search"    
          android:orderInCategory="100"    
          android:showAsAction="never"    
          android:icon="@android:drawable/ic_menu_search"    
          android:title="find" />    
    <item android:id="@+id/view"    
          android:orderInCategory="100"    
          android:showAsAction="never"    
          android:icon="@android:drawable/ic_menu_view"    
          android:title="see" />    
    <item android:id="@+id/help"    
          android:orderInCategory="100"    
          android:showAsAction="never"    
          android:icon="@android:drawable/ic_menu_help"    
          android:title="help" />   
    <item android:id="@+id/exit"    
          android:orderInCategory="100"    
          android:showAsAction="never"    
          android:icon="@android:drawable/ic_menu_revert"    
          android:title="sign out" />    
</menu>    

5, values directory
There are many things in the values directory, including arrays xml/colors. xml/dimens. xml/ids. xml/strings. xml/styles. xml
1.arrays.xml
arrays.xml file is used to store various array data, such as string array, integer array, etc. the data in the array may be specific values or references to resource data.
The data in the tems array is arrays Corresponding resource id in XML file
2.colors.xml
colors. The xml file is mainly used to describe the required color values. You can also create a new color folder in the res directory to store these xml files
3.dimens.xml
dimens.xml is used to define the size of controls and text, which is defined to facilitate screen adaptation
4.ids.xml
ids.xml provides a unique resource id reference for the relevant resources of the application.
5.strings.xml
Android recommends defining the text displayed on the screen in strings XML, and doing so can also achieve internationalization.
6.styles.xml
styles.xml is mainly used to store android themes and styles

Keywords: Java Android xml

Added by boofboof on Tue, 04 Jan 2022 16:46:55 +0200