Android開發-實現底部彈出選單欄
阿新 • • 發佈:2019-02-06
效果圖:
activity_main:
<?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="wrap_content" android:orientation="vertical" android:padding="8dp" tools:context=".MainActivity"> <Button android:id="@+id/btn" android:text="開啟底部對話方塊" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
botton_dialog:
<?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:background="#fff" android:paddingBottom="20dp" android:orientation="vertical"> <Button android:id="@+id/btn_open_camera" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="1" android:textSize="18sp" /> <Button android:id="@+id/btn_choose_img" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="2" android:textSize="18sp" /> <Button android:id="@+id/btn_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="3" android:textSize="18sp" /> </LinearLayout>
因為時彈出,涉及到動畫,所以我們在res下自定義一個資料夾anim
@anim/dialog_in:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromYDelta="100%"
android:toYDelta="0" />
@anim/dialog_iout:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fromYDelta="0" android:toYDelta="100%" />
styles中定義樣式:
<style name="BottomDialog" parent="@android:style/Theme.Dialog">
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<!-- 浮於Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 邊框 -->
<item name="android:windowFrame">@null</item>
<!-- Dialog以外的區域模糊效果 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 無標題 -->
<item name="android:windowNoTitle">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- Dialog進入及退出動畫 -->
<item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>
<!-- ActionSheet進出動畫 -->
<style name="DialogAnimation" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/dialog_in</item>
<item name="android:windowExitAnimation">@anim/dialog_iout</item>
</style>
MainActivity:
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.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_botton_dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_botton_dialog = (Button) findViewById(R.id.btn);
btn_botton_dialog.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
setDialog();
break;
case R.id.btn_choose_img:
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_open_camera:
Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_cancel:
Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
break;
}
}
private void setDialog(){
Dialog mCameraDialog = new Dialog(this, R.style.BottomDialog);
LinearLayout root = (LinearLayout) LayoutInflater.from(this).inflate(
R.layout.botton_dialog, null);
//初始化檢視
root.findViewById(R.id.btn_choose_img).setOnClickListener(this);
root.findViewById(R.id.btn_open_camera).setOnClickListener(this);
root.findViewById(R.id.btn_cancel).setOnClickListener(this);
mCameraDialog.setContentView(root);
Window dialogWindow = mCameraDialog.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
// dialogWindow.setWindowAnimations(R.style.dialogstyle); // 新增動畫
WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 獲取對話方塊當前的引數值
lp.x = 0; // 新位置X座標
lp.y = 0; // 新位置Y座標
lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 寬度
root.measure(0, 0);
lp.height = root.getMeasuredHeight();
lp.alpha = 9f; // 透明度
dialogWindow.setAttributes(lp);
mCameraDialog.show();
}
}
原始碼:https://github.com/luyaopeng/AndroidLearning.git