Android layout_height的属性解释

395 阅读1分钟

安卓layout_height 属性用于确定一个View 组件在特定单位中的高度。

你在布局文件中创建的所有视图组件都需要layout_height 这个属性。

要看一个关于layout_height 属性的例子,这里是一个有三个组件的XML文件:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
 android:layout_height="200dp"
 android:text="Hello World!" />
<Button
android:layout_width="match_parent"
 android:layout_height="100dp"
 android:text="Button" />
</LinearLayout>

LinearLayout 是容器,将layout_height 设置为match_parent 将使其高度与屏幕尺寸一致。

TextViewlayout_height200dpButton 的高度为100dp

设计视图将如下所示:

Android layout_height sizes illustrated

安卓布局_高度的尺寸说明

(android-layout-height.png)

layout_width属性一样,你可以为高度使用五个尺寸单位。dp,in,mm,pt,sp, 和px

dp 这个单位是最值得推荐的,因为它是一个与密度无关的像素,会根据你的屏幕密度相应地调整组件的大小。

有两个特殊的常数你可以用于高度:match_parentwrap_content

match_parent 常数将使用父组件的高度作为视图的高度。最外层的组件将使用屏幕的高度。

wrap_content 常数将根据内容的大小来调整高度。你的内容在垂直方向上越大,高度大小就越大。

这就是layout_height 属性的全部内容。👌