閱讀156 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android 自定義對話框

說到對話框你肯定會想到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);
}
@Override
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();
break;
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);
}
@Override
public void onClick(View v) {
dismiss();//取消
}
}
}


對話框透明

1、定義style:

<resources>

<style
name="dialog_fullscreen">
<item
name="android:windowFullscreen">true</item>
<item
name="android:windowNoTitle">true</item>
<item
name="android:windowBackground">@android:color/transparent</item>
</style>
</resources>

2、定義layout文件:test_dialog.xml

<LinearLayout
android:
xmlns:androhttps://schemas.android.com/apk/res/android">https://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
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(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);



https://blog.csdn.net/iefreer/article/details/4581137

最後更新:2017-04-02 17:51:26

  上一篇:go redis緩存的安裝和使用
  下一篇:go 解決在無線網絡下本機無法連接linux(紅帽)虛擬機問題