說到對話框你肯定會想到alertdialog.builder。當然這次不是用alertdialog.builder來實作的!而是dialog類
alertdialog.builder提供的方法有:
settitle():給對話框設定title.
seticon():給對話框設定圖示。
setmessage():設定對話框的提示資訊
setitems():設定對話框要顯示的一個list,一般用于要顯示幾個指令時
setsinglechoiceitems():設定對話框顯示一個單選的list
setmultichoiceitems():用來設定對話框顯示一系列的複選框。
setpositivebutton():給對話框添加”yes”按鈕。
setnegativebutton():給對話框添加”no”按鈕。
那麼在dialog類怎樣實作的呢?當然是layout啦,你可以自定義一個xml來布置你對話框
看看例子和源碼吧
package com.hl;
import android.app.activity;
import android.app.dialog;
import android.content.context;
import android.os.bundle;
import android.view.gravity;
import android.view.view;
import android.view.window;
import android.view.windowmanager;
import android.widget.button;
public class mydialog extends activity implements android.view.view.onclicklistener {
button btn1=null;
button btn2=null;
button btn3=null;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
btn1=(button)findviewbyid(r.id.b1);
btn2=(button)findviewbyid(r.id.b2);
btn3=(button)findviewbyid(r.id.b3);
btn1.setonclicklistener(this);
btn2.setonclicklistener(this);
btn3.setonclicklistener(this);
}
public void onclick(view v) {
switch(v.getid()){
case r.id.b1:
break;
case r.id.b2:
case r.id.b3:
new mydialogs(this).setdisplay();
default:
class mydialogs extends dialog implements android.view.view.onclicklistener{
private button b1;
private window window=null;
public mydialogs(context context){
super(context);
public void setdisplay(){
setcontentview(r.layout.dialog);//設定對話框的布局
b1=(button)findviewbyid(r.id.clo);
b1.setonclicklistener(this);
setproperty();
settitle("自定義對話框");//設定對話框的标題
show();//顯示對話框
//要顯示這個對話框,隻要建立該類對象.然後調用該函數即可.
public void setproperty(){
window=getwindow();// 得到對話框的視窗.
windowmanager.layoutparams wl = window.getattributes();
wl.x =0;//這兩句設定了對話框的位置.0為中間
wl.y =180;
wl.alpha=0.6f;//這句設定了對話框的透明度
wl.gravity=gravity.bottom;
window.setattributes(wl);
dismiss();//取消
對話框透明
1、定義style:
<resources>
<style
name="dialog_fullscreen">
<item
name="android:windowfullscreen">true</item>
name="android:windownotitle">true</item>
name="android:windowbackground">@android:color/transparent</item>
</style>
</resources>
2、定義layout檔案:test_dialog.xml
<linearlayout
android:id="@+id/ll_dialog"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#000000">
……
</linearlayout>
3、代碼中設定:
public class testdialog extends dialog
{
public testdialog(context context)
super(context, r.style.dialog_fullscreen);
// todo auto-generated constructor stub
protected void oncreate(bundle savedinstancestate)
setcontentview(r.layout.test_dialog);
// 設定背景透明度
view ll = findviewbyid(r.id.ll_dialog);
ll.getbackground().setalpha(120);// 120為透明的比率
擷取layout作為view
方法一:取得layoutinflater
layoutinflater = getlayoutinflater();
方法二:
layoutinflater = layoutinflater.from(this);
方法三:
layoutinflater = (layoutinflater)getsystemservice(layout_inflater_service);
做項目過程中遇到一個問題,從資料庫裡讀取圖檔名稱,然後調用圖檔。直接用r.drawable.?無法調用。查了好多地方最後找到了個方法,分享給大家,希望有幫助。
主要由兩種方法,個人建議第二種。
1. 不把圖檔放在res/drawable下,而是存放在src某個package中(如:com.drawable.resource),這種情況下的調用方法為:
string path = "com/drawable/resource/imagename.png";
inputstream is = getclassloader().getresourceasstream(path);
drawable.createfromstream(is, "src");
2. 如果還是希望直接使用res/drawable中的圖檔,就需要通過下面的方法了:
假設建立工程的時候,填寫的package名字為:com.test.image
int resid = getresources().getidentifier("imagename", "drawable", "com.test.image");
drawable image = getresources().getdrawable(resid);
http://blog.csdn.net/iefreer/article/details/4581137