安卓进阶系列-03上弹选择框(PopupDialog)的使用
生活随笔
收集整理的這篇文章主要介紹了
安卓进阶系列-03上弹选择框(PopupDialog)的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
主要介紹上彈選擇框的使用,這個功能基于Dialog實現,為安卓開發常見控件之一。實現方式并非利用第三方控件而是利用安卓原生對話框控件,不過樣式自定義了。
1.布局使用
<?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="wrap_content"android:background="@android:color/white"android:orientation="vertical"><TextViewandroid:id="@+id/tv_test"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="?android:attr/selectableItemBackground"android:clickable="true"android:drawablePadding="16dp"android:gravity="center_vertical"android:padding="16dp"android:text="第一個選擇"android:textColor="#666666"android:textSize="14sp"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="?android:attr/selectableItemBackground"android:clickable="true"android:drawablePadding="16dp"android:gravity="center_vertical"android:padding="16dp"android:text="第二個選擇"android:textColor="#666666"android:textSize="14sp"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="?android:attr/selectableItemBackground"android:clickable="true"android:drawablePadding="16dp"android:gravity="center_vertical"android:padding="16dp"android:text="第三個選擇"android:textColor="#666666"android:textSize="14sp"/></LinearLayout>2.代碼實現
package com.zc.testforpopupdialog;import android.app.Dialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn = (Button) findViewById(R.id.btn_test);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {showPopupDialog();}});}private void showPopupDialog() {Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);View contentView = LayoutInflater.from(this).inflate(R.layout.dialog, null);bottomDialog.setContentView(contentView);ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();layoutParams.width = getResources().getDisplayMetrics().widthPixels;contentView.setLayoutParams(layoutParams);bottomDialog.getWindow().setGravity(Gravity.BOTTOM);bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);bottomDialog.setCanceledOnTouchOutside(true);bottomDialog.show();// 這里一定要使用對話框的findViewByID,用Activity的將無法捕獲TextView tv = (TextView) bottomDialog.findViewById(R.id.tv_test);tv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this,"你點擊了第一個選項",Toast.LENGTH_LONG).show();}});} }3.效果演示
?
注意:dialog上的控件必須使用該dialog調用findViewByID而不能使用Activity進行綁定,因為這些控件本質上是不存在Activity上的,而是暫存在dialog上。
項目GitHub地址:https://github.com/luanshiyinyang/TestForPopupDialog
總結
以上是生活随笔為你收集整理的安卓进阶系列-03上弹选择框(PopupDialog)的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓进阶系列-02搜索框(Persist
- 下一篇: 机器学习-分类之K近邻算法(KNN)原理