📜  Kotlin 中的 Android 相对布局

📅  最后修改于: 2022-05-13 01:55:41.506000             🧑  作者: Mango

Kotlin 中的 Android 相对布局

Android RelativeLayout是一个 ViewGroup 子类,用于指定子 View 元素之间的相对位置,例如(A 在 B 的右侧)或相对于父级(固定在父级的顶部)。

我们必须使用 RelativeLayout 来设计用户界面并保持层次结构平坦,而不是使用 LinearLayout,因为它可以提高应用程序的性能。

在 RelativeLayout 中定位视图的重要属性

众所周知,我们需要定义子视图或 ViewGroup 相对于其他元素或相对于父元素的位置。默认位置是左上角,如果有人忘记指定子视图的位置。

XML attributesDescription
layout_alignParentLeftIt is set “true” to match the left edge of view to the left edge of parent.
layout_alignParentRightIt is set “true” to match the right edge of view to the right edge of parent.
layout_alignParentTopIt is set “true” to match the top edge of view to the top edge of parent.
layout_alignParentBottomIt is set “true” to match the bottom edge of view to the bottom edge of parent.
layout_alignLeftIt accepts another sibling view id and align the view to the left of the specified view id
layout_alignRightIt accepts another sibling view id and align the view to the right of the specified view id.
layout_alignStartIt accepts another sibling view id and align the view to start of the specified view id.
layout_alignEndIt accepts another sibling view id and align the view to end of specified view id.
layout_centerInParentWhen it is set “true”, the view will be aligned to the center of parent.
layout_centerHorizontalWhen it is set “true”, the view will be horizontally centre aligned within its parent.
layout_centerVerticalWhen it is set “true”, the view will be vertically centre aligned within its parent.
layout_toLeftOfIt accepts another sibling view id and places the view left of the specified view id.
layout_toRightOfIt accepts another sibling view id and places the view right of the specified view id.
layout_toStartOfIt accepts another sibling view id and places the view to start of the specified view id.
layout_toEndOfIt accepts another sibling view id and places the view to end of the specified view id.
layout_aboveIt accepts another sibling view id and places the view above the specified view id.
layout_belowIt accepts another sibling view id and places the view below the specified view id.

如何在 XML 文件中声明 RelativeLayout?

首先,我们应该使用下面的代码在布局文件中声明RelativeLayout。

XML

  
    // Add other view or ViewGroup here


XML


     
    
  
    
      
    
  
    
  
    


Kotlin
package com.geeksforgeeks.myfirstKotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
  
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // below access the UI elements
  
  
    }
}


activity_main.xml 文件中的相对布局

以下是 xml 文件中 RelativeLayout 的代码。

XML



     
    
  
    
      
    
  
    
  
    

MainActivity.kt 文件

创建布局后,我们需要从活动的onCreate()回调方法加载 XML 布局资源,并使用 findViewById 从 XML 访问 UI 元素。

科特林

package com.geeksforgeeks.myfirstKotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
  
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // below access the UI elements
  
  
    }
}

相对布局输出:

我们可以使用 Android 虚拟设备 (AVD) 运行应用程序以获取上述代码的输出。