建立自定義佈局AlertDialog
阿新 • • 發佈:2018-12-30
就手機來說,因為螢幕不夠大,所以在顯示不是必須的資訊時或者不是常用的操作時,我們可以在對話方塊中顯示,但不是傳統的那種簡單的對話方塊,需要自己自定義一下,這樣不僅能節約空間,並且功能也一樣不缺。自定義AlertDialog跟普通的其實差不多,就是多了一些附加操作,使得對話方塊的互動跟activity介面互動一樣,所以今天做了一個簡單的例子。效果如下圖。
因為註釋很詳細所有我少說話多貼程式碼了(懶。。)。
建立對話方塊,但是用的是自己寫的樣式
//用自己寫的樣式替換context的樣式 Context context = new ContextThemeWrapper(this, R.style.customDialog); dialog = new AlertDialog.Builder(context).create(); //設定可點選對話方塊外 對話方塊關閉 dialog.setCanceledOnTouchOutside(true); dialog.setTitle(""); dialog.show();
樣式也挺簡單的就是常規設定,備註很詳細
<style name="customDialog" parent="@android:style/Theme.Dialog"> <!--是否有邊框--> <item name="android:windowFrame">@null</item> <!--是否有標題--> <item name="android:windowNoTitle">true</item> <!--背景顏色--> <item name="android:windowBackground">@color/translucent_background</item> <!--是否浮動--> <item name="android:windowIsFloating">true</item> <!--是否允許背景變暗--> <item name="android:backgroundDimEnabled">true</item> <!--是否透明的--> <item name="android:windowIsTranslucent">true</item> <!--動畫--> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> </style>
既然是自定義,當然得自己設定對話方塊的大小以及位置了
Window window = dialog.getWindow(); //得到螢幕資訊 DisplayMetrics dm = getResources().getDisplayMetrics(); WindowManager.LayoutParams p = window.getAttributes(); //背景變暗 p.dimAmount = 0.1f; p.height = (int) (dm.heightPixels * 0.8); p.width = (int) (dm.widthPixels * 0.8); // 設定對話方塊大小 window.setAttributes(p); //設定顯示位置 window.setGravity(Gravity.LEFT | Gravity.TOP);
最重要的就是新增自己的佈局了,可以自己隨意的新增修改,我只是隨便顯示了一個圖片和兩個按鈕兩個列表
//新增自定義的佈局
window.setContentView(R.layout.view_list_dialog);
lv_1 = window.findViewById(R.id.lv_1);
lv_1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
lv_2 = window.findViewById(R.id.lv_2);
lv_2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
btn_1 = window.findViewById(R.id.btn_1);
btn_2 = window.findViewById(R.id.btn_2);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
dialog.dismiss();
然後在按鈕點選時顯示dialog就行了。
完整activity程式碼如下:
public class DialogActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_show_dialog, btn_1, btn_2;
private ListView lv_1, lv_2;
private AlertDialog dialog;
private List<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
InitDialog();
btn_show_dialog = findViewById(R.id.btn_show_dialog);
btn_show_dialog.setOnClickListener(this);
for (int i = 0; i < 20; i++) {
list.add("資料" + i);
}
}
public void InitDialog() {
//用自己寫的樣式替換context的樣式
Context context = new ContextThemeWrapper(this, R.style.customDialog);
dialog = new AlertDialog.Builder(context).create();
//設定可點選對話方塊外 對話方塊關閉
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle("");
dialog.show();
Window window = dialog.getWindow();
//得到螢幕資訊
DisplayMetrics dm = getResources().getDisplayMetrics();
WindowManager.LayoutParams p = window.getAttributes();
//背景變暗
p.dimAmount = 0.1f;
p.height = (int) (dm.heightPixels * 0.8);
p.width = (int) (dm.widthPixels * 0.8);
// 設定對話方塊大小
window.setAttributes(p);
//設定顯示位置
window.setGravity(Gravity.LEFT | Gravity.TOP);
//新增自定義的佈局
window.setContentView(R.layout.view_list_dialog);
lv_1 = window.findViewById(R.id.lv_1);
lv_1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
lv_2 = window.findViewById(R.id.lv_2);
lv_2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
btn_1 = window.findViewById(R.id.btn_1);
btn_2 = window.findViewById(R.id.btn_2);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
dialog.dismiss();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_1:
Toast.makeText(DialogActivity.this, "你點選了按鈕1", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_2:
Toast.makeText(DialogActivity.this, "你點選了按鈕2", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_show_dialog:
dialog.show();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//別忘了關閉對話方塊
if (dialog!=null&&dialog.isShowing())
dialog.dismiss();
}
}
自定義對話方塊佈局:
<?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:layout_margin="4dp"
android:background="@color/white"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" 列表1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@color/exam_text_color"
/>
<ListView
android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:textColor="@color/exam_text_color">
</ListView>
</LinearLayout>
<TextView
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@color/exam_text_color"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" 列表2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@color/exam_text_color"
/>
<ListView
android:id="@+id/lv_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
</ListView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕1"
/>
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕2"
/>
</LinearLayout>
</LinearLayout>
activity佈局就很簡單了一個按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|bottom"
tools:context=".DialogActivity">
<Button
android:id="@+id/btn_show_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示對話方塊"
/>
</LinearLayout>
到這裡就介紹完了,並且可以使用了,是不是很簡單,自定義對話方塊還有很多很多擴充套件的,發揮你們的想象力吧。