LinearLayout和RelativeLayout布局中使用android:orientation

<?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">     <!--垂直布局-->

    <!--match_parent:当前元素和父类一样;wrap_content:自适应;text:元素中显示的文字内容-->
    <Button
        android:text="button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_1" />

    <Button
        android:text="button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_2" />
</LinearLayout>

效果:

1:线性布局在xml文件中使用<LinearLayout>来定义。

  线性布局可以分为水平和垂直方向的布局,可以通过android:orientation来定义方向

       android:orientation=“horizontal”表示水平方向,android:orientation=“vertical”表示垂直方向。

       

当 android:orientation=”vertical” 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。

当 android:orientation=”horizontal” 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

:2:Android线性布局LinearLayout注意设置属性android:orientation属性,否则有的组件可能无法显示。
该属性不设置时默认为horizontal。

比如上面那段代码我删除了

android:orientation="vertical"

第二个按钮就会被第一个按钮覆盖,显示不出来

3:相对布局在xml文件中使用<RelativeLayout>来定义。

RelativeLayout布局当中使用android:orientation="vertical" ,这个语句不起作用。这句话的意思是布局方式是垂直布局,Relatactivelayout 没这个属性,去掉不会产生影响。

4:android:layout_width表示控件的宽度,android_layout_height表示控件的高度,其属性值有wrap_content、fill_parent、match_parent三种。

其中,wrap_content表示填满父控件的空白,fill_parent表示大小刚好足够显示当前控件里的内容,match_parent与fill_parent作用是相同的。

android:layout_weight表示控件的权重,描述了控件所占的比例有多大。所有的视图都有layout_weight值,其默认为零,表示需要显示多大的视图就占据多大的屏幕空间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

猜你喜欢

转载自blog.csdn.net/weixin_42717928/article/details/83662905