LinearLayout和RelativeLayout布局中使用android:orientation 怼烎@ 2021-09-27 16:44 365阅读 0赞 <?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> 效果: ![20181102232830893.png][] 1:线性布局在xml文件中使用<LinearLayout>来定义。 线性布局可以分为水平和垂直方向的布局,可以通过android:orientation来定义方向 android:orientation=“horizontal”表示水平方向,android:orientation=“vertical”表示垂直方向。 android:layout\_gravity指定控件在布局中的对齐方式 当 android:orientation=”vertical”(垂直) 时, 只有水平方向的对齐方式的设置才起作用,垂直方向的设置不起作用。即:left,right,center\_horizontal 是生效的。 当 android:orientation=”horizontal” (水平)时, 只有垂直方向的对齐方式设置才起作用,水平方向的设置不起作用。即:top,bottom,center\_vertical 是生效的。 :2:Android线性布局LinearLayout注意设置属性android:orientation属性,否则有的组件可能无法显示。 **该属性不设置时默认为horizontal。** **比如上面那段代码我删除了** android:orientation="vertical" 第二个按钮就会被第一个按钮覆盖,显示不出来 ![20181102234248950.png][] 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值中所占的比率而定。 [20181102232830893.png]: /images/20210923/364d07e6627d490eb890b8bfb66b6716.png [20181102234248950.png]: /images/20210923/2b0ce4ca28ba4270aa6fbb17d87d386c.png
还没有评论,来说两句吧...