Layouts

xiaoxiao2022-06-11  15

Layouts

 

Layout资源是通过在XML中设计UI布局来作为显示层的内容,而不是在代码中去构建它们。

 

Layout最常用的用途是为Activity定义UI。一旦在XML中定义,一般就在ActivityonCreate方法中通过setContentView进行显示。

 

你同样可以引用其它的layout资源,例如为ListView的每一行设定layout。更多的关于在Activity中使用和创建layout的信息你可以在第4章中看到。

 

Android中,使用layout来创建屏幕的UI是一个好的习惯。将代码和layout分离开来,有助于你为不同的硬件配置(例如,变化的屏幕大小,方向或者键盘和触摸屏的显示)创建最优的layout

 

每一个layout定义在/res/layout文件夹下的独立的文件里,包含单个layout。文件名就是layout资源的标识。

 

关于layout面板和View元素的详尽解释会在下一章。但作为一个例子,下面的代码片段显示了新工程向导创建的layout。它使用LinearLayout作为一个layout面板,容纳显示“Hello World”问候的TextView

 

<?xml version=”1.0” encoding=”utf-8”?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Hello World!” /> </LinearLayout>

 

转载请注明原文地址: https://www.6miu.com/read-4930693.html

最新回复(0)