android 布局一些细节总结

     初次接触android是在做大学做毕业设计时候,android那时候是很新的一个应用,忙了一个多月,小小的图片浏览器over了,很有感觉,呵呵。 现在又要做android项目了,android此时已与日中天了,发展的真快呀。话不多时,重新接触android时候,发现了一些此前没注意的细节,在此总结一下。

     如果有过做网页的朋友会发现,android的界面跟html,css很像,内容和表现是一体的,因为android没有网页那么复杂的布局。如果能把网页css所有布局对应到android,可以说你淫了(赢了)。

     长度,高度,内边距,背景等这些都和css一样,但是外边距的时候我发现有时候设置了,没反应,后来发现了外边距的设置和父控件的gravity属性有关

    

<?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">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一行"
android:layout_marginBottom="0dip"
/>
</LinearLayout>

看一下结果截图:

很显然看到margin没起作用,我们稍微改一下xml文件

<?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"
  android:gravity="bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一行"
android:layout_marginBottom="50dip"
/>
</LinearLayout>

再看一下结果:

成了,很奇怪,是么?android就是这样弄的,经测试,把那个gravity改成top,left,center等等都不行,规则就是这样,如果控件需要设置外边距,则需要把viewgroup即LineLayout等布局的gravity属性设置成相应的值。

另外,网页中可以设置div占父div的%多少啊,这些可以用android:layout_weight这个属性来实现,具体可以网上百度,这方面很多,不多说,也是今天用到了总结下。        

over,就这样。

原文地址:https://www.cnblogs.com/huals/p/2648180.html