Android基礎TOP5_4:點擊按鈕更換樣式,設定透明度

來源:互聯網
上載者:User

標籤:改變   creat   apk   ack   ima   onclick   generate   大小   tco   

在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         @Override11         public void onClick(View v) {12             // TODO Auto-generated method stub13             //點擊後更改按鈕樣式14             but.setBackgroundResource(R.drawable.newround);15              //另外設定按鈕的透明度setAlpha()裡面的值大小為0~25516             but.getBackground().setAlpha(99);17         }18     });19     }20 }

運行效果:

點擊之前

點擊後:

 

Android基礎TOP5_4:點擊按鈕更換樣式,設定透明度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.