android - 在 Android 的按钮上画一条线?

标签 android button drawing

我正在弄乱 Android 中的日历应用程序,并希望能够在按钮顶部划线或在特定 Y 位置的按钮上放置图像。有没有办法做到这一点?我不认为普通的可绘制对象(左、上、右、下)会给我我正在寻找的位置。 谢谢!

calendar view with images/lines on buttons

更新:

下面是在可绘制按钮上绘制线条的代码的结果。我必须做的是为按钮提供一个透明的 png 图像,用于 XML 代码中的顶部可绘制对象。没有它,它就无法工作。

我在这里看到的一个问题是我只局限于几行。其中一个回复说了一些关于在框架布局上堆叠按钮的事情,但我不确定我是否遵循。将不得不做更多的实验。

有什么方法可以在整个按钮上绘制而不仅仅是小的可绘制区域?

Resules after drawing lines on top drawable

package com.test.buttondraw;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.Button;

public class ButtonDrawTestActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);

        Button b1 = (Button) findViewById (R.id.button1);

        // get the second array element (0 based) which is the top drawable
        BitmapDrawable d = (BitmapDrawable) b1.getCompoundDrawables ()[1];

        if (d != null)
        {   // thanks to dbrettschneider for bitmap code sample
            Bitmap b1src = d.getBitmap ();
            Bitmap tmp = Bitmap.createBitmap(b1src.getWidth(), b1src.getHeight(), b1src.getConfig());
            BitmapDrawable bd = new BitmapDrawable (tmp);
            bd.setBounds (d.getBounds ());

            Canvas c = new Canvas(tmp);

            Paint p = new Paint();
            p.setAntiAlias(true);

            c.drawBitmap (b1src, 0, 0, p);

            // set thickness of line
            p.setStyle (Style.STROKE);
            p.setStrokeWidth (5);

            // colors to use for each y position
            int [] clr    = {Color.BLACK,     Color.RED,     Color.GREEN, 
                                  Color.MAGENTA, Color.WHITE, Color.YELLOW, 
                                  Color.CYAN,        Color.BLUE,   Color.DKGRAY };
            int ypos = 10;

            // draw a different colored line for each element in the ypos array
            for (int i = 0; i < clr.length; i++)
            {
                p.setColor(clr[i]); 
                c.drawLine (0, ypos, b1src.getWidth (), ypos, p);
                ypos += 8;
            }

            b1.setCompoundDrawables (null, bd, null, null);
        }

    }

}

这是xml代码:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/png_transbg"
    android:maxHeight="128dp"
    android:maxWidth="128dp"
    android:minHeight="128dp"
    android:minWidth="128dp"
    android:text="Button" />

最佳答案

如 dtmilano 所说,您可以在 FrameLayout 中堆叠 View 。因此,您可以将一个 Button 放入其中并覆盖第二个 View。在第二个 View 中,您将进行自定义绘图。

但是,如果您只想在按钮文本上方添加彩色线条,那么遵循 Kevin 的建议并采用布局方式会更加优雅。我会这样做:

<LinearLayout android:orientation="vertical"

              style="@android:style/Widget.Button"

              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
  <View android:background="#f00"

        android:layout_margin="1dp"
        android:layout_height="4dp"
        android:layout_width="match_parent"/>
  <View android:background="#0f0"

        android:layout_margin="1dp"
        android:layout_height="4dp"
        android:layout_width="match_parent"/>
  <View android:background="#00f"

        android:layout_margin="1dp"
        android:layout_height="4dp"
        android:layout_width="match_parent"/>
  <TextView android:text="Button"

            android:textAppearance="@android:style/TextAppearance.Widget.Button"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>

当然,您会在代码中添加代表动态行的 View 。您还应该外部化尺寸常数。要将行与按钮文本分开,您可以使用另一个嵌套的 LinearLayout。最后,您可以将所有内容包装到自定义 View 中。

编辑:如果您想将任何可用空间分配给线条,请将它们的 layout_heights 设置为 0dp,并将它们的 layout_weights 设置为 1。

关于android - 在 Android 的按钮上画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8013797/

相关文章:

android - 如何使用末尾带有 # 的 Uri.parse()

asp.net - 将内部 HTML 与 ASP :Button? 一起使用

c# - 在Graphics.Drawing中使用未分配的局部变量

ios - 使用自动布局绘制自定义 UIView 并将其居中作为背景

ios - 在 Swift 中画线

database - 自己调用 onUpgrade 方法

java - android项目中Intellij-Idea的渲染问题

java - 由于动画问题,该应用程序已停止工作

javascript - 为什么单击按钮在我的 Javascript-html 中不起作用?

jQuery 隐藏/显示似乎不起作用 - 我做错了什么