Android對話方塊的進階設定《二》設定對話方塊按鈕的透明度和對話方塊的在螢幕上的顯示位置

來源:互聯網
上載者:User

話不多說,代碼:

XML布局檔案,只是一個Demo,所以就一個Button.

<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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/button_id"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/button_text" /></RelativeLayout>

JAVA代碼:

package com.example.dialog_alpha_demo;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.Button;public class MainActivity extends Activity {private Button mButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mButton = (Button) this.findViewById(R.id.button_id);mButton.setOnClickListener(listener);}@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;}View.OnClickListener listener = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubShowDialog();}};private void ShowDialog() {AlertDialog mDialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher).setTitle("溫馨提示").setMessage("透明對話方塊").setPositiveButton("確定", null).create();// 擷取對話方塊的Window對象Window mWindow = mDialog.getWindow();WindowManager.LayoutParams lp = mWindow.getAttributes();// 透明度的範圍為:0.0f-1.0f;0.0f表示完全透明,1.0f表示完全不透明(系統預設的就是這個)。lp.alpha = 0.35f;//設定對話方塊在螢幕的底部顯示,當然還有上下左右,任意位置//mWindow.setGravity(Gravity.LEFT);mWindow.setGravity(Gravity.BOTTOM);  /*   *    * 這裡是設定位移量,這裡的x,y並不是相對於螢幕的絕對座標,而是相對於對話方塊在中心位置(預設的對話方塊一般顯示在螢幕的中心)而言的lp.x = -20;// 設定水平位移量lp.y = -90;// 設定豎直位移量*/// 設定Window的屬性mWindow.setAttributes(lp);mDialog.show();}}

顯示結果為:

相關文章

聯繫我們

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