Android:带有两个图像的按钮|对齐

Android:带有两个图像的按钮|对齐,android,android-layout,android-button,Android,Android Layout,Android Button,我以编程方式创建了此按钮,并且可以使用下面的代码在上设置图标 现在,我想在右上角有另一个图像(图标),请参见下图: 我已尝试使用以下代码添加这两个图像: Drawable icon = context.getResources().getDrawable(iconResourceId); Drawable icon2 = context.getResources().getDrawable(iconResourceId2); button.setCompoundDrawablesWithIn

我以编程方式创建了此按钮,并且可以使用下面的代码在上设置
图标

现在,我想在右上角有另一个图像(
图标
),请参见下图:

我已尝试使用以下代码添加这两个图像:

Drawable icon = context.getResources().getDrawable(iconResourceId);
Drawable icon2 = context.getResources().getDrawable(iconResourceId2);
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, icon2, null);
我得到以下结果:


谁能告诉我,我怎样才能把它对准右上角

您的代码将如下所示,
btn_background
是您当前的背景

    Button button = new Button(this);
    Drawable icon = context.getResources().getDrawable(iconResourceId);
    button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
    button.setText("Test");

    if (hasNewUpdates()) {
        button.setBackgroundResource(R.drawable.btn_background_with_icon);
    } else {
        button.setBackgroundResource(R.drawable.btn_background);
    }
这就是带有图标的
btn\u背景的样子

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_background"/>
    <item android:gravity="right" android:bottom="72dp" android:drawable="@drawable/ico_info"/>
</layer-list>


尝试在按钮上使用相对图像视图,并将图像设置为imagviewRelativeLayout将执行以下操作:用xml创建一个包含按钮背景和顶部新图标的图层列表。将该图层列表设置为背景button@chappie112,我必须通过编程实现它!你可以用编程的方式替换背景。或者你的意思是什么?你仍然没有明白我的意思,按钮的背景是由渐变色创建的,这两个图像是独立的图像,如果用户是VIP,那么它将显示这两个图像,皇冠图像(右上角图像)和按钮图标(左侧图像),否则它将显示按钮图标(左侧图像)实际上,我想将皇冠图像与右上角对齐!但是这段代码就是这么做的(只需通过VIP检查更改hasNewUpdates()),而不是通过compoundDrawable添加crown图像(我们无法对齐图像),而是通过背景进行添加。另外,按钮是否有渐变也不重要。它可以是任何东西,从图像到带有渐变的xml形状。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_background"/>
    <item android:gravity="right" android:bottom="72dp" android:drawable="@drawable/ico_info"/>
</layer-list>