Ret*_*eto 82 android scale compound-drawables
如何使按钮上的可绘制更小?图标太大,实际上高于按钮.这是我正在使用的代码:
<Button
android:background="@drawable/red_button"
android:drawableLeft="@drawable/s_vit"
android:id="@+id/ButtonTest"
android:gravity="left|center_vertical"
android:text="S-SERIES CALCULATOR"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:drawablePadding="10dp">
</Button>
Run Code Online (Sandbox Code Playgroud)
鞋面是它应该看起来的样子,它现在看起来越低.

我尝试了这个,但没有显示图像.:-(
Resources res = getResources();
ScaleDrawable sd = new ScaleDrawable(res.getDrawable(R.drawable.s_vit), 0, 10f, 10f);
Button btn = (Button) findViewById(R.id.ButtonTest);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null);
Run Code Online (Sandbox Code Playgroud)
Bud*_*ddy 86
我找到了一个非常简单有效的XML解决方案,不需要 ImageButton
为您的图像制作一个可绘制的文件,如下所示并使用它 android:drawableLeft
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/half_overlay"
android:drawable="@drawable/myDrawable"
android:width="40dp"
android:height="40dp"
/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
您可以使用android:width和android:height属性设置图像大小.
这样你至少可以为不同的屏幕获得相同的大小.
缺点是它与fitXY不完全相同,它会缩放图像宽度以适应X并相应地缩放图像高度.
Ron*_*nie 68
您应该使用ImageButton并指定图像android:src,并设置android:scaletype为fitXY
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); //set drawableLeft for example
Run Code Online (Sandbox Code Playgroud)
Ali*_*aka 10
按钮不会调整其内部图像的大小.
我的解决方案不需要代码操作.
它使用TextView和ImageView的布局.
布局的背景应该是红色的3d drawable.
您可能需要定义android:scaleType xml属性.
例:
<LinearLayout
android:id="@+id/list_item"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="2dp" >
<ImageView
android:layout_width="50dp"
android:layout_height="fill_parent"
android:src="@drawable/camera" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:lines="1"
android:gravity="center_vertical"
android:text="Hello - primary" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
BTW:
祝好运
使用ScaleDrawable作为Abhinav建议.
问题是drawable没有显示 - 它是ScaleDrawables中的某种错误.你需要以编程方式更改"级别".这适用于每个按钮:
// Fix level of existing drawables
Drawable[] drawables = myButton.getCompoundDrawables();
for (Drawable d : drawables) if (d != null && d instanceof ScaleDrawable) d.setLevel(1);
myButton.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
Run Code Online (Sandbox Code Playgroud)
我的DiplayScaleHelper完美运行:
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ScaleDrawable;
import android.widget.Button;
public class DisplayHelper {
public static void scaleButtonDrawables(Button btn, double fitFactor) {
Drawable[] drawables = btn.getCompoundDrawables();
for (int i = 0; i < drawables.length; i++) {
if (drawables[i] != null) {
if (drawables[i] instanceof ScaleDrawable) {
drawables[i].setLevel(1);
}
drawables[i].setBounds(0, 0, (int) (drawables[i].getIntrinsicWidth() * fitFactor),
(int) (drawables[i].getIntrinsicHeight() * fitFactor));
ScaleDrawable sd = new ScaleDrawable(drawables[i], 0, drawables[i].getIntrinsicWidth(), drawables[i].getIntrinsicHeight());
if(i == 0) {
btn.setCompoundDrawables(sd.getDrawable(), drawables[1], drawables[2], drawables[3]);
} else if(i == 1) {
btn.setCompoundDrawables(drawables[0], sd.getDrawable(), drawables[2], drawables[3]);
} else if(i == 2) {
btn.setCompoundDrawables(drawables[0], drawables[1], sd.getDrawable(), drawables[3]);
} else {
btn.setCompoundDrawables(drawables[0], drawables[1], drawables[2], sd.getDrawable());
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98601 次 |
| 最近记录: |