【问题标题】:drawable right for toggle button切换按钮的可绘制右侧
【发布时间】:2014-01-10 02:50:00
【问题描述】:

我在 xml 中为切换按钮上的可绘制右侧设置了一个圆形(形状)。 circle.xml 的初始颜色为绿色,但我无法让它显示在按钮上。当我使用图像时,我能够看到图像,所以我确定问题出在圆形或形状上。 这是他用可绘制的权利切换按钮。

<ToggleButton
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_weight="1"
    android:background="@drawable/custom_fixture_buttons"
    android:textColor="@drawable/white"
    android:textOff="F1"
    android:textOn="F1"
    android:drawableRight="@drawable/circle"
    android:textSize="30sp" />

这是 circle.xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/circle"
    android:shape="oval"
    android:color="#ff00FFFF" >
</shape>

我总共有 10 个带有可绘制权限的切换按钮,需要分别更改每个按钮的颜色,这是设置每个颜色点的方法。第一代在每个按钮上使用了单独的图像,但我需要更改代码以将其应用于切换按钮 drawableright。我尝试了 setCompoundDrawablesWithIntrinsicBounds 但出现错误。

switch (index) {
    case 0: {
        Resources res = getResources();
        final Drawable drawable = res.getDrawable(R.drawable.circle);
        drawable.setColorFilter(Color.rgb(i, j, k), Mode.SRC_ATOP);
        // ImageView img = (ImageView) findViewById(R.id.colordot1);
        // img.setBackgroundDrawable(drawable);
        // Fixture1.setCompoundDrawablesWithIntrinsicBounds(0, 0,img, 0);
        break;
}

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    您错误地创建了可绘制对象。

    这是一个创建彩色圆圈的可绘制示例:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
    
        <solid 
            android:color="#00FFFF" />
    
    </shape>
    

    【讨论】:

    • 我做了更改,但圆圈仍然没有出现在切换按钮中
    • 也许有更好的方法来做你想做的事。您能解释一下您要完成的工作吗?图片和布局代码帮助。
    • 我正在尝试添加一个彩色圆圈做按钮与drawableright。我需要用 RGB 颜色控制圆形填充颜色。我发布了上面的切换按钮代码和用于更改颜色的代码。圈子没有出现
    • 不要打扰drawableRight。只需将您的 ToggleButton 嵌套在设置为水平方向的 LinearLayout 中,并在其中包含您的圆圈的 ImageView。
    • 我已经试过了。我需要按钮内的圆圈。这是我谈到需求的帖子。stackoverflow.com/questions/20926283/…