【问题标题】:Adding a background image, to a background xml layout将背景图像添加到背景 xml 布局
【发布时间】:2013-01-18 09:51:11
【问题描述】:

我正在使用可绘制的 xml 文件来使我的应用程序中的按钮“漂亮”。

^ 就这样。

它工作正常,但是我想在按钮上添加一个勾号或叉号(以标记完成与否) - 我不太确定它是否可能。

这是我的按钮 xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp" />
        </shape>
    </item>
</selector>

我将按钮上的背景元素设置为使用上述元素,如@drawable/bluebuttons

任何帮助将不胜感激

【问题讨论】:

  • 如果你想从activity动态处理这个问题,我发布了解决方案。

标签: java android drawable


【解决方案1】:

如果您使用的是按钮,那么您可以在 XML 文件中使用 Button 的属性。

android:drawableRight="@drawable/ANY_DRAWABLE_IMAGE"

在我的情况下是这样的:

<Button android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
    ...
    android:drawableRight="@drawable/ic_tick" />

或者在你的代码中,你可以这样写:

// PUT ZERO FOR NO DRAWABLE
textView1.setCompoundDrawablesWithIntrinsicBounds(LEFT_DRAWABLE, TOP_DRAWABLE,RIGHT_DRAWABLE, BOTTOM_DRAWABLE);

希望这会对你有所帮助。

【讨论】:

  • 你先生,是个传奇。非常感谢 - 我会尽可能接受它
【解决方案2】:

如果您有兴趣从Activity 动态设置按钮上的图像,那么您可以使用这个:

Drawable iconTick= getApplicationContext().getResources().getDrawable(R.drawable.icon_tick);
mBtnTaks1.setCompoundDrawablesWithIntrinsicBounds(iconTick, null, null, null); // left,top,right,bottom

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-06
    • 2012-04-09
    • 2011-06-01
    • 2017-01-14
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多