android - 在 Android 应用程序中创建这样的 Button 的最佳实践?

标签 android xml android-layout android-view

我刚刚开始使用 Eclipse 和 ADT 创建 Android 应用程序。
我已经在演示应用程序中获得了基本功能,并且想创建一个看起来像搜索字段的按钮(没有内置搜索功能,我只希望用户按下它并开始新 Activity )。

http://www.roowilliams.com/button.png

我的问题是:创建上述按钮的最佳做法是什么(忽略外部周围的 1px 深灰色描边)?

我已经做到了

  1. 在activity_main.xml 中创建按钮

    <Button
        android:id="@+id/inputSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="@string/hint_search"
        android:background="@drawable/input_search"
        style="@style/input_search"
        android:onClick="findProducts" >
    </Button>
    
  2. 创建一些样式

    <style name="input_search" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">@color/light_grey</item>
        <item name="android:background">@color/white</item>
    </style>
    
  3. 创建 input_search_background.xml,它具有浅灰色笔划和圆角

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <stroke 
            android:width="@dimen/stroke_width" android:color="@color/light_grey" /> 
        <corners 
            android:radius="@dimen/corner_radius" />
        <solid 
            android:color="@color/white" />
    </shape>
    
  4. 为状态创建 input_search.xml。

    <?xml version="1.0" encoding="utf-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/input_search_background"
            android:state_pressed="true" />  
    
        <item android:drawable="@drawable/input_search_background"
            android:state_focused="true" />
    
        <item android:drawable="@drawable/input_search_background" />
    
    </selector>
    
  5. 我不确定如何创建右侧的橙色 block 。我应该定义一个橙色的形状并为放大镜使用透明的 png 还是 png 应该是带有放大镜的橙色 block ?

您使用什么工具来创建 Assets ?
我如何知道创建它们的大小?

我将不胜感激任何有帮助的智慧之言或链接。

谢谢,
安德鲁

最佳答案

您可以简单地使用 9 patch .

您可以在您的 sdk 平台图形中找到一个类似的:

在 Windows 中(安装路径可能会改变),

C:\Program Files\adt-bundle-windows-x86_64-20130522\sdk\platforms\android-8\data\res\drawable-mdpi

你可以找到这个:

enter image description here

通过一些(非常少的)工作,您可以制作出满足您需求的完美图像。
只需将它添加为 Button 的背景(我会使用 TextView,但这是个人喜好问题)并享受。

你看,不需要自定义样式,也不需要额外的 xml 可绘制对象。

关于android - 在 Android 应用程序中创建这样的 Button 的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753175/

相关文章:

c# - 在 Silverlight 中编辑 XML 文件不起作用,但为什么?

android - 嵌入另一个线性布局的线性布局

java - 如何在 Android 的菜单选项中添加带有文本的图像?

android - 在 ConstraintLayout 中以编程方式删除/添加约束

android - android 中的 ScrollView 与 ListView

java - 整个文档和片段都用一个 XSD 吗?

android - 如何在 ListView 中的某些项目之后显示分隔符?

javascript - 在 XML 文件中存储和检索新行 '\n'

android - calendar.set(Calendar.HOUR_OF_DAY, alarmHour);不管用。我究竟做错了什么?

android - 安卓应用的源代码?