使用代码为Button设置drawableTop和修改文字颜色

来源:互联网 发布:扫描服务器开放的端口 编辑:程序博客网 时间:2024/05/18 03:59

在布局文件中经常看到:

<Button            android:id="@+id/fg"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@null"            android:drawableTop="@drawable/home_unactive"            android:onClick="click"            android:text="首页"            android:textColor="#ffffff" />

将布局文件中drawableTop属性在代码中表示为:

Drawable dr = getResources().getDrawable(R.drawable.background);

dr.setBounds(0, 0, dr.getMinimumWidth(), dr.getMinimumHeight());

fg.setCompoundDrawables(left, top, right, bottom);

修改文字颜色:

fg.setTextColor(getResources().getColor(R.color.blue));

0 1