Android設定視窗變暗,圓角按鈕以及include和merge的使用,androidmerge

來源:互聯網
上載者:User

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







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.