什么是视图的方向常量?

时间:2016-07-15 03:38:07

标签: android android-view

<View
    android:layout_width="match_parent"
    android:layout_height="12dp"
    android:orientation="horizontal" />

上面的xml中android:orientation="horizontal"是什么?

Android-Studio快速提示显示它是方向常量,这不是很有帮助。

3 个答案:

答案 0 :(得分:8)

android:orientation仅与ViewGroup相关,其子项是连续组织的,例如LinearLayout,因此在您的情况下,它绝对没用。

答案 1 :(得分:3)

在视图中没有使用方向属性,只有线性布局或其他父布局需要方向

答案 2 :(得分:2)

方向常量主要用于视图组,它显示该视图组的子组应该如何排列。由于view是所有ViewGroups的父类

参见截图

enter image description here

说明是here

假设您有任何LinearLayout,并且您希望该布局内的内容以垂直方式设置,那么您可以将此方向常量用作垂直方向,并将该LinearLayout中的所有组件用于垂直方向

实施例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>

在相对布局中,它可以如此处所述实现,

https://developer.android.com/guide/topics/ui/layout/relative.html

但在父类视图中,这根本没用;