Androidbutton,xml文件定义形状,代码中修改背景颜色-创新互联

1. 首先在drawable文件夹定义一个shape.xml文件,内容如下:

目前成都创新互联公司已为成百上千的企业提供了网站建设、域名、虚拟主机绵阳服务器托管、企业网站设计、广安网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

<?xml version="1.0" encoding="UTF-8"?>
<shape
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
   <solid android:color="#e6e6" />

   <corners
       android:topLeftRadius="10dp"
       android:topRightRadius="10dp"
       android:bottomRightRadius="10dp"
       android:bottomLeftRadius="10dp" />

   <padding
       android:left="30dp"
       android:top="0dp"
       android:right="30dp"
       android:bottom="0dp"
       />
</shape>

2. 在main.xml文件中,button使用这个shape.xml,如下:

<Button
       android:id="@+id/button_next"
       android:layout_height="wrap_content"
       android:layout_width="match_parent"
       android:text="@string/button_next"
       android:textSize="@dimen/label_text_size"
       android:layout_alignParentBottom="true"
       android:layout_centerVertical="true"
       android:gravity="center_horizontal|center_vertical"
       android:layout_marginLeft="15dp"
       android:layout_marginRight="15dp"
       android:background="@drawable/shape"
       />

这时,该button就显示如shape.xml定义的形状。

3. 在代码中动态修改button 背景颜色,代码如下:

GradientDrawable bgShape = (GradientDrawable)buttonNext.getBackground();
bgShape.setColor(Color.BLUE);

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享题目:Androidbutton,xml文件定义形状,代码中修改背景颜色-创新互联
网站URL:https://www.cdcxhl.com/article8/ddddip.html

成都网站建设公司_创新互联,为您提供用户体验网页设计公司建站公司服务器托管品牌网站建设软件开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站建设