三種方式展示透明界面


本文將介紹三種展示透明界面的方法:

第1種    通過展示透明的popwindow



這種方式適合邏輯處理較少的,比如選擇一些東西

但是有以下缺點:

首先,如果裏邊有Editext  用的話會出現一個問題   因爲一旦點擊輸入,軟鍵盤會將popwindow中的view向上推 

    等到關閉的時候   你會發現剛纔被=推上去的view並沒有隨着軟鍵盤的隱藏向下移動 

   也就是說本來我這個界面在底部  輸入完成後   界面就顯示到了中間   我嘗試找但是一直沒找到辦法
其次,還有一個問題(代碼中動態添加時)  展示popwindow一般在點擊事件中展示,

    因爲popwindow創建需要view,這個view我只知道在點擊事件中有  一般都是在點擊事件中 

   但這種情況不在   我解析一個二維碼   解析完成後顯示一個結果

   那這個結果的顯示並不在點擊事件   也就缺少創建對象時構造方法中的那個view  那這種情況就不能用了

PopupWindow popupWindow = new PopupWindow(popupWindow_view,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);

下邊先看popwindow的佈局文件:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:gravity="center"
            android:text="這是透明的popwindow"
            android:textSize="20dp"
            android:layout_marginTop="130dp" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_centerInParent="true"
            android:id="@+id/hide"
            android:text="隱藏透明popwindow"
            android:textSize="20dp" />
</RelativeLayout></span>
注意: popupwindow佈局文件不用設置透明顏色

再看MainActivity中

<span style="font-size:14px;">// 顯示 設置logo的popupWindow
	private void showPopWindow(View v) {
		popupWindow_view = initPopwindowView();
		popupWindow = new PopupWindow(popupWindow_view,
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
		// popupWindow.setAnimationStyle(R.anim.slide_in_from_bottom);
		popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
		popupWindow_view.setFocusable(true);
		popupWindow.setFocusable(true);
		popupWindow_view.setFocusableInTouchMode(true);

		// 設置透明度
		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.alpha = 0.5f;
		getWindow().setAttributes(params);

		// 點擊其他地方消失
		popupWindow_view.setOnTouchListener(new OnTouchListener() {
			@SuppressLint("ResourceAsColor")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				closePopwindow();
				return false;
			}
		});

		// 點擊返回鍵讓 popupWindow消失
		popupWindow_view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					closePopwindow();
					return true;
				}
				return false;
			}
		});

	}

	// 初始化popupWindow
	private View initPopwindowView() {
		View view = View.inflate(this, R.layout.popupwindow, null);
		Button hide = (Button) view.findViewById(R.id.hide);
		hide.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				closePopwindow();	
			}
		});
		return view;
	}

	// 隱藏popupWindow
	private void closePopwindow() {
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow_view.setFocusable(false);
			popupWindow_view.setFocusableInTouchMode(false);
			popupWindow.setFocusable(false);

			popupWindow.dismiss();
			popupWindow = null;
			WindowManager.LayoutParams params = getWindow().getAttributes();
			params.alpha = 1f;
			getWindow().setAttributes(params);
		}
	}</span>
第2種       通過展示透明的activity


思路:   開啓一個新的activity,在清單文件中將這個activity的風格改成透明的

1.現在style下添加一個style 


    <span style="font-size:14px;">   <style name="Theme.Translucent" parent="AppBaseTheme">
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
    </style></span>
2.剛纔的style裏邊有一個顏色 下邊在color下添加一個顏色


接着在清單文件中找到要打開的透明activity中添加activity的風格  
<span style="font-size:14px;"><activity
            android:name=".TransparentActivity"
            android:label="@string/title_activity_transparent"
            android:theme="@android:style/Theme.Translucent" >
        </activity>
</span>


3.我們在新的activity中添加一些控件

<span style="font-size:14px;"><RelativeLayout 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="#90000000"
    tools:context="${relativePackage}.${activityClass}" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginTop="130dp"
        android:gravity="center"
        android:text="這是開啓的透明Activity"
        android:textSize="20dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:onClick="close"
        android:text="關閉"
        android:textSize="20dp" />
</RelativeLayout></span>
4.這個時候開啓這個透明的activity就可以出現如圖透明的界面了


第3中種   通過view來顯示透明界面

  思路   在相對佈局的最下邊寫一個佈局  但這個佈局是透明的   佈局中可以添加我們想要添加的東西,但是默認讓它gone掉

            在我們想要開啓的再讓它visible

先說佈局文件


<span style="font-size:14px;"><RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="這是普通Activity"
        android:textSize="20dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:onClick="show"
        android:text="顯示透明view"
        android:textSize="20dp" />
    <RelativeLayout
        android:id="@+id/rl_tran"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#90000000"
        android:visibility="gone" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:gravity="center"
            android:text="這是透明的view"
            android:textSize="20dp"
            android:layout_marginTop="130dp" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_centerInParent="true"
            android:onClick="hide"
            android:text="隱藏透明view"
            android:textSize="20dp" />
    </RelativeLayout>
</RelativeLayout></span>
     注意:裏邊的相對佈局是透明的外邊的相對佈局不能透明

再說MainActivity中

<span style="font-size:14px;"> private RelativeLayout rl_tran;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		rl_tran = (RelativeLayout) findViewById(R.id.rl_tran);
	}

	// 點擊後顯示透明界面
	public void show(View view) {
		rl_tran.setVisibility(View.VISIBLE);
	}

	// 點擊後隱藏透明界面
	public void hide(View view) {
		rl_tran.setVisibility(View.GONE);
	}</span>

點擊這裏下載通過透明的activity展示透明的界面源碼  


點擊這裏下載通過透明的View展示透明的界面源碼  





發佈了34 篇原創文章 · 獲贊 24 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章