我在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.我知道这很愚蠢,但我需要在活动中这样做,因为我在那里创建了按钮.我如何在活动中的xml文件中执行相同的操作?我需要添加drawableLeft和drawable padding以及padding left.
这就是我在活动中创建按钮的方式
Button button1 = new Button(this); button1.setLayoutParams(new RelativeLayout.LayoutParams(buttonWidth,buttonHeight)); button1.setText(systemTexts.getShowCallButton()); button1.setBackgroundDrawable(new button1.setTextColor(Color.parseColor(buttonTextColor));
解决方法
Drawable image = getContext().getResources().getDrawable( R.drawable.icon ); image.setBounds( 0,60,60 ); button.setCompoundDrawables( image,null,null );
做这个
更新:
由于getContext().getResources().getDrawable现已弃用,请改用:
Drawable image = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon); image.setBounds( 0,60 ); button.setCompoundDrawables( image,null );
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。