Button适配多种android屏幕

    开发android也快一年了,虽然只是实习。本人的博文只是分享个人的经验,有错误欢迎指出,同时也希望能帮上一些刚刚入门的同学,如果你已经是大牛了,请路过。谢谢!

   好吧,回归正题,开发android过程中,屏幕适配是必不可少的工作,因为android手机五花八门,没有统一的标准,屏幕大小,质量,dp(不知道dp的同学该去查找一下,dp和dip是一样的)和像素的计算单位也可能不一致。 那么我们一个应用通常都有导航栏按钮,通常这个导航栏都是横向铺满屏幕的,而且每个按钮Button的比例大小都一样。这时要做屏幕适配,我用了一个比较投机取巧的方法大笑真的比较投机取巧。我用了透明的背景图片用作按钮的背景图片。然后设置weight权重为1,其实有了这个再设置width的意义不大。如我下面截图所示。

       当然这里可能会有人要考虑内存的使用,没错,一个应用最多能使用的内存一般都是是64M,超过这个应用就会崩溃。这里我们只是讨论一下导航栏的屏幕适配方案,而且只是一张不大的透明图片,对整个应用的的内存影响不大,因为一般导航栏都需要一直显示,这个图片自然也就会常驻内存,如果没有做特别的处理,一般来说,不管显示了多少个按钮,应该都是使用同一个内存的图片。

   好吧,第一篇博文就先说这多吧,期望以后能每天一篇博文。


    ButtonDemo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是具有屏幕适配效果的 button 按钮的示例代码: ```xml <Button android:id="@+id/my_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/my_button_text" android:textColor="@color/my_button_text_color" android:textSize="@dimen/my_button_text_size" android:padding="@dimen/my_button_padding" android:background="@drawable/my_button_background" /> ``` 其中,`android:layout_width="match_parent"` 表示宽度会自动适配父容器的宽度,而 `android:layout_height="wrap_content"` 表示高度会根据内容自动适配。 `android:textSize` 和 `android:padding` 属性可以使用 `dp` 或 `sp` 单位来适配不同的屏幕密度。 `android:background` 属性可以引用一个自定义的 drawable,来为按钮设置背景样式。可以根据不同的屏幕密度,提供不同的 drawable,以保证按钮在各种屏幕上看起来都很好看。例如: ```xml res/drawable-mdpi/my_button_background.xml res/drawable-hdpi/my_button_background.xml res/drawable-xhdpi/my_button_background.xml res/drawable-xxhdpi/my_button_background.xml res/drawable-xxxhdpi/my_button_background.xml ``` 这些 drawable 文件可以分别定义不同的背景样式,例如: ```xml <!-- res/drawable-mdpi/my_button_background.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/my_button_background_color" /> <corners android:radius="@dimen/my_button_corner_radius" /> </shape> ``` ```xml <!-- res/drawable-hdpi/my_button_background.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/my_button_background_color" /> <corners android:radius="@dimen/my_button_corner_radius" /> <stroke android:width="@dimen/my_button_stroke_width" android:color="@color/my_button_stroke_color" /> </shape> ``` 这些文件中,可以根据需要设置不同的颜色、圆角半径、边框宽度和颜色等属性,来达到适配不同屏幕的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值