自定义View

//自定义弹窗
public class MyDialog {
    private Context mContext;
    private Dialog mDialog;
    private TextView mTitle;
    private TextView mCancel;
    private LinearLayout mLinearLayout;
    private ScrollView mScrollView;
    private boolean isShowTitle = false;
    private Display mDisplay;
    private ArrayList<SheetItem> mSheetItemList;


    @SuppressLint("ServiceCast")
    public MyDialog(Context context) {
        this.mContext = context;
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mDisplay = manager.getDefaultDisplay();
    }

    public MyDialog Builder() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_actionsheet, null);
        view.setMinimumWidth(mDisplay.getWidth());
        mScrollView = (ScrollView) view.findViewById(R.id.sLayout_content);
        mLinearLayout = (LinearLayout) view.findViewById(R.id.lLayout_content);
        mTitle = (TextView) view.findViewById(R.id.txt_title);
        mCancel = (TextView) view.findViewById(R.id.txt_cancel);


        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        mDialog.setContentView(view);

        Window window = mDialog.getWindow();
        window.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;
        lp.y = 0;
        window.setAttributes(lp);
        return this;
    }

    public MyDialog setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            isShowTitle = true;
            mTitle.setText(title);
            mTitle.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public MyDialog seTCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    public MyDialog setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public void show() {
        setSheetItem();
        mDialog.show();
    }

    private void setSheetItem() {
        if (mSheetItemList == null || mSheetItemList.size() < 1) {
            return;
        }
        int size = mSheetItemList.size();
        if (size > 6) {
            ViewGroup.LayoutParams params = mLinearLayout.getLayoutParams();
            params.height = mDisplay.getHeight() / 2;
            mScrollView.setLayoutParams(params);
        }
        for (int x = 1; x <= size; x++) {
            final int index = x;
            final SheetItem sheetItem = mSheetItemList.get(x - 1);

            TextView textView = new TextView(mContext);
            textView.setText(sheetItem.name);
            textView.setTextSize(18);
            textView.setGravity(Gravity.CENTER);
            if (size == 1) {
                if (isShowTitle) {
                    textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                } else {
                    textView.setBackgroundResource(R.drawable.actionsheet_top_selector);
                }
            } else {
                if (isShowTitle) {
                    if (x >= 1 && x < size) {
                        textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
                    } else {
                        textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                    }
                } else {
                    if (x == 1) {
                        textView.setBackgroundResource(R.drawable.actionsheet_top_selector);
                    } else if (x < size) {
                        textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
                    } else {
                        textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                    }
                }
            }
            if (sheetItem.color != null) {
                textView.setTextColor(Color.parseColor(sheetItem.color.getName()));
            } else {
                textView.setTextColor(Color.parseColor(SheetItemColor.BULE.getName()));
            }

            float density = mContext.getResources().getDisplayMetrics().density;
            int heigth = (int) (45 * density + 0.5);
            textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, heigth));
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sheetItem.listener.onClick(index);
                    mDialog.dismiss();
                }
            });
            mLinearLayout.addView(textView);
        }
    }

    public MyDialog addSheetItem(String name, SheetItemColor color, OnSheetItemClickListener listener) {
        if (mSheetItemList == null) {
            mSheetItemList = new ArrayList<>();
        }
        mSheetItemList.add(new SheetItem(name, listener, color) {
        });
        return this;
    }

    private abstract class SheetItem {
        String name;
        OnSheetItemClickListener listener;
        SheetItemColor color;

        public SheetItem(String name, OnSheetItemClickListener listener, SheetItemColor color) {
            this.name = name;
            this.listener = listener;
            this.color = color;
        }
    }

    public interface OnSheetItemClickListener {
        void onClick(int witch);
    }

    public enum SheetItemColor {
        BULE("#037BFF"), RED("#FD4A2E");
        String name;

        private SheetItemColor(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}



public class MainActivity extends AppCompatActivity {
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn= (Button) findViewById(R.id.btn);
     btn.setOnClickListener(new View.OnClickListener() {
     @Override            
     public void onClick(View v) {  
            new MyDialog(MainActivity.this).Builder().addSheetItem("拍照", MyDialog.SheetItemColor.BULE, new MyDialog.OnSheetItemClickListener() {                            @Override                            public void onClick(int witch) {                                                            }                        }).addSheetItem("打开相册", MyDialog.SheetItemColor.BULE, new MyDialog.OnSheetItemClickListener() {                    @Override                    public void onClick(int witch) {                                           }                }).show();            }        });    }

layout里文件

layout_activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ccc">

    <Button
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
</RelativeLayout>

layout_view_actionsheet.xml

<?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:orientation="vertical"
    android:padding="8dp">

    <TextView
        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:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="10dp"
        android:textColor="@color/actionsheet_gray"
        android:textSize="13sp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/sLayout_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none">

        <LinearLayout
            android:id="@+id/lLayout_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"/>
    </ScrollView>

    <TextView
        android:id="@+id/txt_cancel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/actionsheet_single_selector"
        android:gravity="center"
        android:text="取消"
        android:textColor="@color/actionsheet_blue"
        android:textSize="18sp" />
</LinearLayout>


styles.xml

<style parent="@android:style/Theme.Dialog" name="ActionSheetDialogStyle">
    <!-- 背景透明 -->
    <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/ActionSheetDialogAnimation</item>
</style>

<!-- ActionSheet进出动画 -->
<style parent="@android:style/Animation.Dialog" name="ActionSheetDialogAnimation">
    <item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item>
    <item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item>
</style>

color.xml

<color name="actionsheet_blue">#037BFF</color>
<color name="actionsheet_red">#FD4A2E</color>
<color name="actionsheet_gray">#8F8F8F</color>

drawable里文件

actionsheet_dialog_in.xml

<?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" />

actionsheet_dialog_out.xml

<?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%" />
actionsheet_bottom_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/actionsheet_bottom_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/actionsheet_bottom_normal"/>

</selector>
actionsheet_middle_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/actionsheet_middle_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/actionsheet_middle_normal"/>

</selector>

actionsheet_single_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/actionsheet_single_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/actionsheet_single_normal"/>

</selector>
actionsheet_top_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/actionsheet_top_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/actionsheet_top_normal"/>

</selector>
actionsheet_bottom_normal.9.png

actionsheet_bottom_pressed.9.png

//将此图片拖至桌面,直接解压,可得所需.9图

actionsheet_middle_normal.9.png

actionsheet_middle_pressed.9.png

actionsheet_single_normal.9.png


actionsheet_single_pressed.9.png


actionsheet_top_normal.9.png

actionsheet_top_pressed.9.png







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值