Android設定視窗變暗,圓角按鈕以及include和merge的使用,androidmerge
昨天寫了一個小Demo,實現了幾個小功能,今天貼上來。由於這幾個個Feature比較簡單,所以放在一起了。先看一下:
上代碼:
Main:
package com.example.includelayoutdemo;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.Toast;public class Main extends Activity {private Button btnSet;private Button btnCancel;private WindowManager.LayoutParams layoutParams;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btnSet = (Button) findViewById(R.id.btnSet);btnCancel = (Button) findViewById(R.id.btnCancel);btnSet.setOnClickListener(new onClickListenerImp());btnCancel.setOnClickListener(new onClickListenerImp());layoutParams = getWindow().getAttributes();}class onClickListenerImp implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v == btnSet) {// 將視窗變暗處理layoutParams.dimAmount = 0.7f;// 0.0~1.0layoutParams.alpha = 0.6f;// 0.0全黑 ~1.0原視窗getWindow().setAttributes(layoutParams);Toast.makeText(Main.this, "Set Dark", Toast.LENGTH_SHORT).show();} else if (v == btnCancel) {// 取消視窗變暗layoutParams.dimAmount = 1.0f;layoutParams.alpha = 1.0f;getWindow().setAttributes(layoutParams);Toast.makeText(Main.this, "Cancel Dark", Toast.LENGTH_SHORT).show();}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
以下是一堆布局:
main.xml
<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:background="#40ff50" android:orientation="vertical" tools:context="com.example.includelayoutdemo.Main" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/zj_round_btn_more" android:gravity="center" android:text="Main" android:textColor="#80ffffff" android:textSize="80dp" /> <include android:id="@+id/inc_other" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/other" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/zj_round_btn_more" android:gravity="center" android:text="Main" android:textColor="#80ffffff" android:textSize="80dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" > <Button android:id="@+id/btnSet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:background="@drawable/zj_round_btn" android:padding="5dp" android:text="Set Dark" android:textColor="#80ffffff" /> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/zj_round_btn_more" android:padding="5dp" android:text="Cancel Dark" android:textColor="#80ffffff" /> </LinearLayout></LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/zj_round_btn_more" android:gravity="center" android:text="Include-Other" android:textColor="#80ffffff" android:textSize="80dp" /> <include android:id="@+id/inc_other" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/another" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/zj_round_btn_more" android:gravity="center" android:text="Include-Other" android:textColor="#80ffffff" android:textSize="80dp" /></LinearLayout>
another.xml
<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" android:background="#ffff00" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/zj_round_btn_more" android:gravity="center" android:text="Merge-Another" android:textColor="#80ffffff" android:textSize="80dp" /></merge>
還有形狀檔,圓角矩形背景:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#30ffffff" /> <corners android:radius="7dp" /></shape>
轉載請註明出處:周木水的CSDN部落格 http://blog.csdn.net/zhoumushui
我的GitHub:周木水的GitHub https://github.com/zhoumushui