Kotlin 中的 Android 相对布局
Android RelativeLayout是一个 ViewGroup 子类,用于指定子 View 元素之间的相对位置,例如(A 在 B 的右侧)或相对于父级(固定在父级的顶部)。
我们必须使用 RelativeLayout 来设计用户界面并保持层次结构平坦,而不是使用 LinearLayout,因为它可以提高应用程序的性能。
在 RelativeLayout 中定位视图的重要属性
众所周知,我们需要定义子视图或 ViewGroup 相对于其他元素或相对于父元素的位置。默认位置是左上角,如果有人忘记指定子视图的位置。
XML attributes | Description |
---|---|
layout_alignParentLeft | It is set “true” to match the left edge of view to the left edge of parent. |
layout_alignParentRight | It is set “true” to match the right edge of view to the right edge of parent. |
layout_alignParentTop | It is set “true” to match the top edge of view to the top edge of parent. |
layout_alignParentBottom | It is set “true” to match the bottom edge of view to the bottom edge of parent. |
layout_alignLeft | It accepts another sibling view id and align the view to the left of the specified view id |
layout_alignRight | It accepts another sibling view id and align the view to the right of the specified view id. |
layout_alignStart | It accepts another sibling view id and align the view to start of the specified view id. |
layout_alignEnd | It accepts another sibling view id and align the view to end of specified view id. |
layout_centerInParent | When it is set “true”, the view will be aligned to the center of parent. |
layout_centerHorizontal | When it is set “true”, the view will be horizontally centre aligned within its parent. |
layout_centerVertical | When it is set “true”, the view will be vertically centre aligned within its parent. |
layout_toLeftOf | It accepts another sibling view id and places the view left of the specified view id. |
layout_toRightOf | It accepts another sibling view id and places the view right of the specified view id. |
layout_toStartOf | It accepts another sibling view id and places the view to start of the specified view id. |
layout_toEndOf | It accepts another sibling view id and places the view to end of the specified view id. |
layout_above | It accepts another sibling view id and places the view above the specified view id. |
layout_below | It 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) 运行应用程序以获取上述代码的输出。
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。