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

时间: 2024-08-29 11:06:59

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

android 带边框的圆角按钮

新建buttonstyle.xml 代码如下 <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 连框颜色值 --><item> <shape> <solid android:color="#1

Android Shape自定义纯色圆角按钮

版权声明:分享技术,传播快乐.如果本博客对你有帮助,请在我的博客首页为我打赏吧! 在Android开发中,为响应美化应用中控件的效果,使用Shape定义图形效果,可以解决图片过多的问题. 首先看一下效果图: 整个页面布局为: 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res

Android设置虚线、圆角、渐变

有图又真相,先上图再说. 点击效果: 设置虚线: [html] view plain copy <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" > <stroke android:dashGap=

[安卓]Android 设置窗口全屏

转自:http://blog.sina.com.cn/s/blog_4c451e0e010133ab.html 设置全屏包括两个部分: 窗口全屏和Activity全屏.窗口全屏是指隐藏系统顶部用来显示时间.电量.信号等信息的标题栏:Activity全屏是指隐藏程序的标题栏.我们可以通过修改AndroidManifest.xml文件来实现. 1.窗口全屏fullscreen.java代码如下: package wzhnsc.test.style; import android.app.Activi

Android设置AlertDialog点击按钮对话框不关闭(转)

(转自:http://blog.csdn.net/winson_jason/article/details/8485524) 当我们在用到Android alertDialog创建对话框 的时候,我们会遇到一个问题就是:我们添加的按钮不论是用setNegativeButton还是用setPositiveButton添加的按钮,点击的时候,都会关闭对话框,但是我们的一 些实际需求,就需要保留这个对话框不动,例如输入校验码,密码之类的校验问题,如果用户输入错误,而关闭后弹出对话框,就很别扭了. 在网

Android设置窗口、控件透明度

设置透明效果的方法如下: 1.在布局文中设置透明效果 android:background="@android:color/transparent" (通过android自带颜色设置成透明) android:background="#e0000000" (通过颜色值,设置成半透明) android:background="#00000000" (通过颜色值,设置成全透明) android:alpha="0.5"       

Qt 之 设置窗口边框的圆角(使用QSS和PaintEvent两种方法)

Qt在设置窗口边框圆角时有两种方式,一种是设置样式,另一种是在paintEvent事件中绘制窗口.下面分别叙述用这两种方式来实现窗口边框圆角的效果. 一.使用setStyleSheet方法 this->setStyleSheet(“QWidget{border-top-left-radius:15px;border-top-right-radius:5px;}”)); 使用的主要是使用border-radius 属性,关于这个属性,可选的样式有 border-top-left-radius 设置

Android布局优化之include、merge、ViewStub的使用

本文针对include.merge.ViewStub三个标签如何在布局复用.有效减少布局层级以及如何可以按需加载三个方面进行介绍的. 复用布局可以帮助我们创建一些可以重复使用的复杂布局.这种方式也意味着应用中任何在多个布局文件之间使用的通用布局都可以被提取出来,然后分别进行管理,使用的时候再进行组合.因此当我们在自定义一些View的时候,使用复用布局会更简单方便.在平常开发中使用可以复用的布局文件,不仅仅是因为它可以有效减少布局文件数量,更多的目的在于它更方面我们管理应用,布局复用,在更改某个组

Android如何设置圆角按钮

1. 在res目录下的drawable目录下新建shape.xml文件 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 填充的颜色 --> <solid and