• Android基础TOP5_4:点击按钮更换样式,设置透明度


    在res/drawable创建两个样式 点击前/点击后

    round:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <shape xmlns:android="http://schemas.android.com/apk/res/android">
     3   <!--设置渐变颜色 angle="0"是从左到右  90是从上到下  180是从右到左 -->
     4     <gradient 
     5         android:startColor="#4169E1"
     6         android:endColor="#E6E6FA"
     7         android:angle="0" />
     8     <stroke 
     9         android:width="2dp"
    10         android:color="#D2691E"
    11         />
    12     <corners android:radius="10dip"/>
    13     <padding 
    14         android:left="10dp"
    15            android:top="10dp"
    16               android:right="10dp"
    17                  android:bottom="10dp"
    18         />
    19 </shape>

    newround:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <shape xmlns:android="http://schemas.android.com/apk/res/android">
     3   <!-- angle="0"是从左到右  90是从上到下  180是从右到左 且必须是45的倍数 -->
     4     <gradient 
     5         android:startColor="#FFD700"
     6         android:endColor="#f5deb3"
     7         android:angle="0" />
     8     <stroke 
     9         android:width="2dp"
    10         android:color="#FFB6C1"
    11         />
    12     <corners android:radius="20dip"/>
    13     <padding 
    14         android:left="10dp"
    15            android:top="10dp"
    16               android:right="10dp"
    17                  android:bottom="10dp"
    18         />
    19 </shape>

    接下来j

    Activity:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" 
        android:background="@drawable/asdf"
        tools:context="com.example.top5_4.MainActivity" >
    
       
    <Button 
        android:id="@+id/but"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="点击按钮改变按钮样式"
         android:background="@drawable/round"
        />
    </LinearLayout>
    

      JAVA:

     1 public class MainActivity extends Activity {
     2 private Button but;
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7        but=(Button) findViewById(R.id.but);
     8     but.setOnClickListener(new OnClickListener() {
     9         
    10         @Override
    11         public void onClick(View v) {
    12             // TODO Auto-generated method stub
    13             //点击后更改按钮样式
    14             but.setBackgroundResource(R.drawable.newround);
    15              //另外设置按钮的透明度setAlpha()里面的值大小为0~255
    16             but.getBackground().setAlpha(99);
    17         }
    18     });
    19     }
    20 }

    运行效果:

    点击之前

    点击后:

  • 相关阅读:
    Ucloud的自主研发的检测主机是否被入侵的agent
    logstash 中多行合并
    python yield的解释
    influxdb 配置文件注释
    supervisor 完整安装步骤
    Linux创建系统用户
    kafka 集群的部署安装
    shell 计算时间差
    phantomjs 的安装部署
    yarn 的安装
  • 原文地址:https://www.cnblogs.com/AndroidCSY/p/6684054.html
Copyright © 2020-2023  润新知