【问题标题】:Android Custom button with imageview and textview inside?内部带有imageview和textview的Android自定义按钮?
【发布时间】:2017-07-09 19:01:22
【问题描述】:

我正在寻找创建一个自定义按钮。理想情况下,此按钮在左侧有一个图像,在右侧有一个文本视图。我将如何做到这一点?

【问题讨论】:

    标签: java android


    【解决方案1】:

    最快的方式

    创建可点击的视图,以可绘制的按钮为背景,包含 ImageView 和 TextView。

    <RelativeLayout
        android:clickable="true"
        android:background="@android:drawable/btn_default"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/image"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@android:drawable/ic_input_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </ImageView>
        <TextView
            android:id="@+id/text"
            android:text="Sample text"
            android:textAppearance="?android:attr/textAppearanceButton"
            android:layout_toRightOf="@id/image"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
    </RelativeLayout>
    

    其他方式

    创建自己的类,扩展RelativeLayout,如上所述设置内容视图,添加自定义方法来设置文本、图像等,仅此而已。 使用您的自定义布局。

    【讨论】:

      【解决方案2】:

      您可以将此类代码用于该任务:

      <Button
       android:layout_width="fill_parent"
       android:layout_height="50dp"
       android:id="@+id/btnLogin"
       android:text="@string/text"
       android:gravity="center"
       android:drawableLeft="@drawable/your_image"
       android:background="@drawable/login"/> // this is for nice background - *.9.png
      

      【讨论】:

        【解决方案3】:

        使用您想要的布局创建一个 xml,创建一个看起来像您希望按钮外观的可绘制对象,将可绘制对象作为背景添加到线性布局并将 xml 膨胀到自定义视图。

        可绘制示例:

          <?xml version="1.0" encoding="utf-8"?> 
          <selector xmlns:android="http://schemas.android.com/apk/res/android">     
        <item android:state_pressed="true" >         
            <shape>             
                <solid android:color="#f3ae1b" />             
                <stroke android:width="1dp" android:color="#bb6008" />             
                <corners android:radius="3dp" />             
                <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />         
            </shape>     
        </item>     
        <item>         
            <shape>             
                <gradient 
                    android:startColor="#f3ae1b" 
                    android:endColor="#bb6008" 
                    android:angle="270" />             
                <stroke 
                    android:width="1dp" 
                    android:color="#bb6008" />             
                <corners 
                    android:radius="4dp" />             
                <padding 
                    android:left="10dp" 
                    android:top="10dp" 
                    android:right="10dp" 
                    android:bottom="10dp" />         
            </shape>     
        </item> 
          </selector> 
        

        编辑:要膨胀的代码

        private LayoutInflater mInflater;
        mInflater = LayoutInflater.from(this);
        public LinearLayout yourButton;
        yourButton = (LinearLayout)  mInflater.inflate(R.xml.yourButton, null);
        

        【讨论】:

        • 我一直使用那个代码,两位反对者会这么好心解释一下吗?
        • 我没有投反对票,但恕我直言,这对他的要求来说是不必要的复杂化。为什么要使用布局充气器和xml布局,如果他可以只使用一个按钮标签并在左侧设置一个drawable,请参阅下面Anton Derevyanko的回复。
        • 没错,我什至不知道drawableLeft的值,在各种情况下都非常有用,但有时我非常喜欢自定义视图的膨胀,因为能够实现各种功能和行为。
        • drawableLeft 属性不是很有用,它不能正确缩放,而且你无法控制它。
        • 另外,请确保在 LinearLayout 上设置 android:clickable="true" - 仅将其设置为背景时效果很好;虽然我不知道你想用 LayoutInflator 完成什么——没有那个点,一切似乎都很好。
        【解决方案4】:

        在 Xml 布局中使用这个可绘制的布局选项并获得简单的布局。下面的行给出了文本左侧的图像。

        android:drawableLeft="@drawable/image"

        【讨论】:

          【解决方案5】:

          如果您想编写代码来执行此操作,请使用 Button 类的 setCompoundDrawables() 来确定可绘制对象相对于文本的显示位置。 查找 API 以了解更多详细信息。

          【讨论】:

            【解决方案6】:

            您可以使用 NoboButton 来做到这一点

            使用图标、半径、背景创建 android 按钮的简单快速方法

            图书馆网址https://github.com/alex31n/NoboButton

            截图https://raw.githubusercontent.com/alex31n/NoboButton/master/Assets/button_screenshot.jpg

            【讨论】:

            • 请在帖子中至少包含您建议的要点 - (外部)链接可能会过时。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多