【问题标题】:android button with background image and text inside带有背景图像和文本的 android 按钮
【发布时间】:2014-07-07 12:46:04
【问题描述】:

如何创建这样的按钮?

我想稍后在应用程序中以编程方式更改文本数量。

【问题讨论】:

  • 你能发一些代码吗?
  • 您可以使用TextView。它是可点击的。它可以包含动态文本。它可以有背景,而不使用按钮的默认背景。

标签: android image button background


【解决方案1】:

创建该背景的图像并获取一个文本视图,将其背景设置为此图像并设置您想要设置的任何文本。

例如你的这个图片名称是 abc.png

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/abc"
    android:text="1" />

设置动态值可以使用TextView的setText方法

【讨论】:

    【解决方案2】:

    您可以创建一个包含您的图像和文本的布局(例如 framelayout),并在该布局上添加一个侦听器

    <FrameLayout
        android:id="@+id/my_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/my_image"/>
        <TextView
            android:id="@+id/my_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        </FrameLayout>
    
    
    (TextView) findViewById(R.id.my_text).setText("HELLO");
    
    findViewById(R.id.my_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Action on click
            }
        });
    

    希望对你有帮助!

    【讨论】:

      【解决方案3】:

      在您的 Button XML 代码中使用此属性。

      android:background="@android:color/transparent"
      android:drawableTop="@drawable/[your background image here]"
      

      【讨论】:

        【解决方案4】:

        你应该在这里使用 Textview:

        像这样设置TextView的属性:

             android:padding_left="20dp"
             android:poadding_right="20dp"
             android:padding_top="5dp"
             android:padding_bottom="5dp"
             android:textColor="#000000"
             android:backgroundColor="#FFFFFF"
             android:clickable="true"
             android:textSize="22sp"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-11-14
          • 2012-09-03
          • 2011-10-11
          • 1970-01-01
          • 2015-02-07
          • 1970-01-01
          • 2013-10-08
          相关资源
          最近更新 更多