排列方向
这段代码可以实现竖直排列。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1"/>
</LinearLayout>
下面的代码运行没有问题,但是为什么没有竖直排列?
<LinearLayout
android:layout_orientation = "vertical"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content">
<TextView
android:text="Hi there!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:fontFamily="sans-serif-light"
android:textColor="@android:color/black"
android:background="#ccddff"
android:padding="20dp"/>
<TextView
android:text="Hi there!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:fontFamily="sans-serif-light"
android:textColor="@android:color/black"
android:background="#ccddff"
android:padding="20dp"/>
</LinearLayout>
将前一段代码的三行
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >~~~
替换第二段代码的那三行,OK。
![可以vertical了](http://upload-images.jianshu.io/upload_images/2994271-86d99e8c050e6972.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/240)
差别是:
android:orientation="vertical" >对的;
android:layout_orientation = "vertical"错的
当然排列方向可以横排horizental或者竖排vertical
####高度宽度
视图的高度、宽度都可以是固定的、wrap_content和match_parent
试验如下代码:
<LinearLayout
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation="vertical" >
<TextView
android:text="Hi there!"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="36sp"
android:fontFamily="sans-serif-light"
android:textColor="@android:color/black"
android:background="#ccddff"
android:padding="20dp"/>
<TextView
android:text="Hi there!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:fontFamily="sans-serif-light"
android:textColor="@android:color/black"
android:background="#ccddff"
android:padding="20dp"/>
<TextView
android:text="Hi there!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="36sp"
android:fontFamily="sans-serif-light"
android:textColor="@android:color/black"
android:background="#ccddff"
android:padding="20dp"/>
<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#1cddff"
android:textSize="36sp"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1cddff"
android:textSize="36sp"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:background="#1cddff"
android:text="Button 3"/>
</LinearLayout>
![好乱](http://upload-images.jianshu.io/upload_images/2994271-080ea32a08896330.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)
如果想要均衡地将这些视图分布在device上,简单暴力地这样固定长度高度或者match_parent、wrap_content是
行不通的。我们去搜索一下LinearLayout有没有办法实现均衡布局?
let's Google search for a potential solution to the problem.
![可以得到一大堆结果](http://upload-images.jianshu.io/upload_images/2994271-17d74fb8b9750c54.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)
[这个网站](http://stackoverflow.com)在你需要其他人帮助时,可以查找问题答案,别人可能已经遇到了同样的问题,并且提出了答案。或者贴出问题,让别人来帮助你。网站手机打不开那也没办法!
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2994271-a4582ff494d22634.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)
提示我们可以使用layout_weight属性。