我在xml文件中创建了如下按钮:
<Button
android:id="@+id/call_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="30dp"
android:background="@drawable/button"
android:layout_weight="40"
android:drawableLeft="@drawable/symbol_phone"
android:paddingLeft="20dp"
android:drawablePadding="-25dp"
android:text="Call"
android:textColor="@color/white"
/>
我想知道如何在活动中做drawableLeft。我知道这很愚蠢,但我需要在活动中这样做,因为我在那里创建了按钮。我怎样才能在我的活动中做同样的事情呢?我需要添加drawableLeft和可绘制的填充和填充左边。这就是我如何在activity中创建按钮
Button button1 = new Button(this);
button1.setLayoutParams(new RelativeLayout.LayoutParams(buttonWidth, buttonHeight));
button1.setText(systemTexts.getShowCallButton());
button1.setBackgroundDrawable(new
button1.setTextColor(Color.parseColor(buttonTextColor));
发布于 2012-05-14 09:14:26
Drawable image = getContext().getResources().getDrawable( R.drawable.icon );
image.setBounds( 0, 0, 60, 60 );
button.setCompoundDrawables( image, null, null, null );
执行此操作
更新:
由于getContext().getResources().getDrawable
现在是不推荐使用的,请改用以下代码:
Drawable image = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);
image.setBounds( 0, 0, 60, 60 );
button.setCompoundDrawables( image, null, null, null );
发布于 2012-05-14 09:15:15
尝尝这个,
Drawable icon= getContext().getResources().getDrawable(R.drawable.icon);
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
发布于 2012-05-14 09:13:37
以编程方式设置drawableLeft的方法是使用setCompoundDrawablesWithIntrinsicBounds。
您可以使用setPadding设置填充
这些文档显示了所有xml标签的相应java方法。
https://stackoverflow.com/questions/10580380
复制相似问题