模擬QQDialog底部彈出

先上效果圖

此圖借鑒了可可DJ音樂網(wǎng)


2、編寫布局


android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:padding="8dp" >

android:id="@+id/txt_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/actionsheet_top_normal"

android:gravity="center"

android:minHeight="45dp"

android:paddingTop="10dp"

android:paddingBottom="10dp"

android:paddingLeft="15dp"

android:paddingRight="15dp"

android:textColor="@color/actionsheet_gray"

android:textSize="13sp"

android:visibility="gone" />

android:id="@+id/sLayout_content"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:fadingEdge="none"

>

android:id="@+id/lLayout_content"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/txt_cancel"

android:layout_width="match_parent"

android:layout_height="45dp"

android:layout_marginTop="8dp"

android:background="@drawable/alertdialog_single_selector"

android:gravity="center"

android:text="取消"

android:textColor="#037BFF"

android:textSize="18sp"

android:textStyle="bold" />

3、主要代碼

package com.luobo.app.widget;

import java.util.ArrayList;

import java.util.List;

import com.luobo.app.R;

import android.app.Dialog;

import android.content.Context;

import android.graphics.Color;

import android.view.Display;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.view.WindowManager;

import android.widget.LinearLayout;

import android.widget.LinearLayout.LayoutParams;

import android.widget.ScrollView;

import android.widget.TextView;

public class ActionSheetDialog {

private Context context;

private Dialog dialog;

private TextView txt_title;

private TextView txt_cancel;

private LinearLayout lLayout_content;

private ScrollView sLayout_content;

private boolean showTitle = false;

private List sheetItemList;

private Display display;

public ActionSheetDialog(Context context) {

this.context = context;

WindowManager windowManager = (WindowManager) context

.getSystemService(Context.WINDOW_SERVICE);

display = windowManager.getDefaultDisplay();

}

public ActionSheetDialog builder() {

// 獲取Dialog布局

View view = LayoutInflater.from(context).inflate(

R.layout.view_actionsheet, null);

// 設(shè)置Dialog最小寬度為屏幕寬度

view.setMinimumWidth(display.getWidth());

// 獲取自定義Dialog布局中的控件

sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content);

lLayout_content = (LinearLayout) view

.findViewById(R.id.lLayout_content);

txt_title = (TextView) view.findViewById(R.id.txt_title);

txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);

txt_cancel.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//dialog.dismiss();

}

});

// 定義Dialog布局和參數(shù)

dialog = new Dialog(context, R.style.ActionSheetDialogStyle);

dialog.setContentView(view);

Window dialogWindow = dialog.getWindow();

dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);

WindowManager.LayoutParams lp = dialogWindow.getAttributes();

lp.x = 0;

lp.y = 0;

dialogWindow.setAttributes(lp);

return this;

}

public ActionSheetDialog setTitle(String title) {

showTitle = true;

txt_title.setVisibility(View.VISIBLE);

txt_title.setText(title);

return this;

}

public ActionSheetDialog setCancelable(boolean cancel) {

dialog.setCancelable(cancel);

return this;

}

public ActionSheetDialog setCanceledOnTouchOutside(boolean cancel) {

dialog.setCanceledOnTouchOutside(cancel);

return this;

}

/**

*

* @param strItem

* ? ? ? ? ? ?條目名稱

* @param color

* ? ? ? ? ? ?條目字體顏色,設(shè)置null則默認(rèn)藍(lán)色

* @param listener

* @return

*/

public ActionSheetDialog addSheetItem(String strItem, SheetItemColor color,

OnSheetItemClickListener listener) {

if (sheetItemList == null) {

sheetItemList = new ArrayList();

}

sheetItemList.add(new SheetItem(strItem, color, listener));

return this;

}

/** 設(shè)置條目布局 */

private void setSheetItems() {

if (sheetItemList == null || sheetItemList.size() <= 0) {

return;

}

int size = sheetItemList.size();

// TODO 高度控制,非最佳解決辦法

// 添加條目過多的時(shí)候控制高度

if (size >= 7) {

LinearLayout.LayoutParams params = (LayoutParams) sLayout_content

.getLayoutParams();

params.height = display.getHeight() / 2;

sLayout_content.setLayoutParams(params);

}

// 循環(huán)添加條目

for (int i = 1; i <= size; i++) {

final int index = i;

SheetItem sheetItem = sheetItemList.get(i - 1);

String strItem = sheetItem.name;

SheetItemColor color = sheetItem.color;

final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener;

TextView textView = new TextView(context);

textView.setText(strItem);

textView.setTextSize(18);

textView.setGravity(Gravity.CENTER);

// 背景圖片

if (size == 1) {

if (showTitle) {

textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);

} else {

textView.setBackgroundResource(R.drawable.actionsheet_single_selector);

}

} else {

if (showTitle) {

if (i >= 1 && i < size) {

textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);

} else {

textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);

}

} else {

if (i == 1) {

textView.setBackgroundResource(R.drawable.actionsheet_top_selector);

} else if (i < size) {

textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);

} else {

textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);

}

}

}

// 字體顏色

if (color == null) {

textView.setTextColor(Color.parseColor(SheetItemColor.Blue

.getName()));

} else {

textView.setTextColor(Color.parseColor(color.getName()));

}

// 高度

float scale = context.getResources().getDisplayMetrics().density;

int height = (int) (45 * scale + 0.5f);

textView.setLayoutParams(new LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, height));

// 點(diǎn)擊事件

textView.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

listener.onClick(index);

dialog.dismiss();

}

});

lLayout_content.addView(textView);

}

}

public void show() {

setSheetItems();

dialog.show();

}

public interface OnSheetItemClickListener {

void onClick(int which);

}

public class SheetItem {

String name;

OnSheetItemClickListener itemClickListener;

SheetItemColor color;

public SheetItem(String name, SheetItemColor color,

OnSheetItemClickListener itemClickListener) {

this.name = name;

this.color = color;

this.itemClickListener = itemClickListener;

}

}

public enum SheetItemColor {

Blue("#037BFF"), Red("#FD4A2E");

private String name;

private SheetItemColor(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

}

用了他家的圖,怎么說也要回饋一下他吧,送上鏈接一條,表示尊敬。

是不是“”干貨",就看你怎么想了,我覺得慢慢的回憶感

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,510評(píng)論 0 17
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,098評(píng)論 25 708
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,491評(píng)論 2 45
  • 云計(jì)算,英文單詞是Cloud Computing ,顧名思義,在云的平臺(tái)進(jìn)行處理計(jì)算。拿到劉鵬老師著作的云計(jì)算第...
    IFBUT閱讀 293評(píng)論 0 2
  • 昨日與今日在福州開辦《打造高績效團(tuán)隊(duì)》公開課,這是在福州開辦的團(tuán)隊(duì)教練首期新課,做課前準(zhǔn)備的時(shí)候,很是期待,前兩次...
    特舒閱讀 165評(píng)論 0 1