Android对于界面底部N个按钮平分屏幕宽度且保持按钮背景图片不变形的解决方案

来源:互联网 发布:入骨相思知不知书包网 编辑:程序博客网 时间:2024/06/05 00:28

我们有时候会用到类似桌面底部那样的导航栏,但是有些背景图片不能做成.9.png格式的,如果我们采用Button的方式来实现,会存在很多问题,比如背景图片被拉伸变形导致界面很难看,而且如果在布局文件中用layout_marginLeft或者layout_marginRight这样的属性,也不容易适配不同屏幕的分辨率,如果采用layout_weight这样的属性导致的问题依然是图片被拉伸,而且各个按钮之间的距离也会很难调。

废话就说这么多,解决方案:使用TextView,使用Textview中的drawableTop属性,这样的设置既不会导致图片被拉伸也不会导致适配分辨率困难。下一给出一段简单的布局文件的代码


首先是主要布局中的导航栏的代码:


<LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_marginBottom="6dp"        android:gravity="center_horizontal"        android:orientation="horizontal" >        <TextView            android:id="@+id/firstBtn"            style="@style/ButtonBar"            android:drawableTop="@drawable/myfee_selector" />        <TextView            android:id="@+id/secondBtn"            style="@style/ButtonBar"            android:drawableTop="@drawable/mypackage_selector" />        <TextView            android:id="@+id/thirdBtn"            style="@style/ButtonBar"            android:drawableTop="@drawable/mylife_selector" />        <TextView            android:id="@+id/forthBtn"            style="@style/ButtonBar"            android:drawableTop="@drawable/myphone_selector" />        <TextView            android:id="@+id/fifthBtn"            style="@style/ButtonBar"            android:drawableTop="@drawable/mymobile_selector" />    </LinearLayout>

style.xml

  <style name="ButtonBar">        <item name="android:clickable">true</item>        <item name="android:layout_width">0dp</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_weight">1</item>    </style>


好了,就这些简单的代码就可以实现一个比较容易适配屏幕的底部导航栏了。


原创粉丝点击