阅读280 返回首页    go 阿里云 go 技术社区[云栖]


关于Android中界面XML文件的绘制顺序

在开发Android程序的时候经常会遇到写界面XML文件的问题,其中感触最深的就是一些控件显示不出来或者显示的位置不对。通过不断地调试终于发现了一些原因,现在写出来,希望可以帮到大家。

  之前我写过一个很简单的界面:

<?xmlversion="1.0"encoding="utf-8" ?>
<LinearLayoutxmlns:andro
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
 <RelativeLayoutxmlns:andro
   android:layout_width="fill_parent"
  android:layout_height="fill_parent">
 <ListViewandroid:
 android:layout_height="wrap_content"
   android:layout_width="fill_parent"
   android:paddingTop="15dp"
   android:paddingBottom="15dp"
 android:layout_alignParentTop="true"  />
  <Buttonandroid:
   android:layout_width="fill_parent"
   android:layout_height="60dp"
   android:text="设置"
   android:gravity="center"
   android:textSize="15sp"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/listview_mibTree"/>
  </RelativeLayout>
</LinearLayout>





  但是这样的界面如果ListView的内容太长的话,底部的按钮就消失了。这是因为,系统在解析这个.XML文件的时候应该是顺序解析顺序绘制的一个过程,根据这个XML文件的逻辑,会先绘制ListView,之后在绘制底部的按钮,言下之意就是先绘制ListView,如果ListView的内容过多,那么势必为需要向下滑动浏览,ListView自己的内容都展示不完,哪里又还有位置留给底部的按钮呢?
  所以正确的逻辑应该是:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:andro
   android:layout_width="fill_parent"
  android:layout_height="fill_parent"
    android:orientation="vertical">
   <RelativeLayoutxmlns:andro
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
        >
          <Button
           android:
           android:layout_width="fill_parent"
          android:layout_height="60dp"
            android:text="设置"
            android:gravity="center"
            android:textSize="15sp"
          android:layout_alignParentBottom="true"/>
       <ListView
           android:
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
            android:paddingTop="15dp"
          android:paddingBottom="15dp"
           android:layout_alignParentTop="true"
          android:layout_above="@id/btn_setInMain"
            ></ListView>
 
    </RelativeLayout>
 
</LinearLayout>





 

这时先解析的是按钮控件,所以已经现将其绘制到了界面的底部,这时就算ListView的内容再多,也不会影响Button的显示。而且其中ListView有一行代码:
android:layout_above="@id/btn_setInMain"
所以在编写界面的时候,只要搞清楚了其绘制的顺序,有很多问题就可以解决了。

表明ListView是绘制在Button之上的,所以该ListView不会把Button覆盖掉。

最后的界面:


所以只要弄清楚了其绘制的顺序,有的问题就容易解决了。

最后更新:2017-04-03 18:51:52

  上一篇:go 拿什么拯救你,我的大规模杀伤性武器--Nokia!
  下一篇:go 下一代网络:大道至“简”