在按钮中缩放可绘制对象?

13

目前,我的可绘制对象只是按比例缩放到其正常大小,我希望它适合我的按钮内。以下是当前的样式图片:

enter image description here

这是按钮的 XML 代码:

<LinearLayout android:layout_width="wrap_content"
    android:id="@+id/linearLayout2" android:layout_height="40dp"
    android:layout_weight="0.4" android:gravity="right|center_vertical"
    android:paddingRight="5dp">
    <Button android:id="@+id/button1" android:background="@drawable/refresh"
        android:layout_height="25dp" android:layout_width="150dp"
        android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48"></Button>
</LinearLayout>

所以我希望它看起来像这样:

在此输入图片描述

有没有办法缩小我的可绘制对象?

6个回答

3
您可以通过编程解决此问题:请查看这个答案中的scaleButtonDrawables函数。使用该函数,您可以在您的活动的onCreate()方法中编写以下内容:
final Button button = (Button) findViewById(R.id.button1);
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                button.getViewTreeObserver().removeOnPreDrawListener(this);
                //scale to 90% of button height
                DroidUtils.scaleButtonDrawables(button, 0.9);
                return true;
            }
        });

由于在onCreate()中无法获取按钮高度,因此您不能直接调用此方法。


谢谢!运行正常。 - DmitryKanunnikoff

2

由于您无法通过 XML 修改或缩放图像,因此需要在代码中设置复合绘制边界。

通过覆盖 Button 类创建自己的 "CustomButton"。然后覆盖方法 "SetCompoundDrawables(left, top, right, bottom)":

    public override void SetCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
    {
        if (right != null)
        {
            var bounds = right.Bounds;
            right.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        if (left != null)
        {
            var bounds = left.Bounds;
            left.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        base.SetCompoundDrawables(left, top, right, bottom);
    }

请注意,由于我使用了Xamarin,所以这是C#代码。但是Java代码应该是相同的(除了一些大写命名)。


2

我认为我找到了解决方案,虽然有点晚,但对其他人可能有帮助。

Drawable drawable = getResources().getDrawable(R.drawable.s_vit);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5), 
                     (int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); 

1
重新制作这张图片。保持像素大小不变,但缩小箭头的尺寸并使其余部分透明。这是我的猜测 =)

看起来我得选择那个。 - soren.qvist
1
为什么像素比dip更好? - IgorGanapolsky
我认为你误解了我的回答:我并不是说像素比dip更好。当我写“保持图像大小以像素为单位...[]”时,我的意思是实际的.png图像而不是某个xml元素。 - Emir Kuljanin

-1

尝试

<Button android:id="@+id/button1" android:background="@drawable/refresh"
    android:layout_height="25dp" android:layout_width="150dp"
    android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48"
    android:padding="5dp"></Button>

(所以添加 android:padding="5dp"

-1

我成功了!虽然有点凌乱,但最终我根本没有使用按钮视图。 "按钮" 本身是一个相对布局,因此如果您想使用此解决方案,则需要调整一些内容,以便使按钮动画起来。这是我的 XML:

<LinearLayout android:layout_width="wrap_content"
    android:id="@+id/linearLayout2" android:layout_height="40dp"
    android:layout_weight="0.4" android:gravity="right|center_vertical"
    android:paddingRight="5dp">
    <RelativeLayout android:layout_height="25dp"
        android:layout_width="150dp" android:id="@+id/relativeLayout1"
        android:clickable="true">
        <ImageView android:id="@+id/imageView1" android:scaleType="fitXY"
            android:adjustViewBounds="true" android:layout_width="fill_parent"
            android:src="@drawable/saywhat" android:layout_height="fill_parent"></ImageView>
        <LinearLayout android:layout_width="fill_parent"
            android:id="@+id/linearLayout3" android:weightSum="1"
            android:layout_height="fill_parent">
            <TextView android:layout_weight="0.6"
                android:layout_height="fill_parent" android:text="TextView"
                android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
            <LinearLayout android:layout_weight="0.4"
                android:layout_height="fill_parent" android:layout_width="wrap_content"
                android:id="@+id/linearLayout4">
                <ImageView android:id="@+id/imageView2"
                    android:layout_height="wrap_content" android:scaleType="fitXY"
                    android:adjustViewBounds="true" android:layout_width="wrap_content"
                    android:src="@drawable/refresh_48"></ImageView>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

享受 :)


10
这是一个非常非常不必要复杂的按钮 :P - Jona
1
那将是一个太邪恶的按钮。 - Amit Gupta
8
经过多年的Android经验后,我可以肯定地说,你甚至不应该考虑以这种方式进行实现。 - soren.qvist

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接