首页 > 代码库 > Android设置窗口变暗,圆角按钮以及include和merge的使用

Android设置窗口变暗,圆角按钮以及include和merge的使用

昨天写了一个小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;
	

	@Override
	protected 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 {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v == btnSet) {
				// 将窗口变暗处理
				layoutParams.dimAmount = 0.7f;	// 0.0~1.0
				layoutParams.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();
			}
		}
	}

	@Override
	public 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;
	}

	@Override
	public 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




Android设置窗口变暗,圆角按钮以及include和merge的使用