Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

加载底部自定义Dialog

$
0
0
  • 创建自定义Dialog布局的xml文件,位置vendor/mdiatek/proprietary/packages/apps/Gallery2Zuk/res/layout/delete_photos_dialog_layout.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:background="#fafafa"
        android:gravity="center"
        android:orientation="vertical" >
        
        <TextView 
            android:id="@+id/delete_photos_dialog_message"
            android:layout_width="match_parent"
            android:layout_height="52dip" 
            android:gravity="center"
            android:textColor="#999999"
            android:textSize="14sp"
            android:layout_marginTop="11dp"
            android:layout_marginBottom="5dp"
            android:background="#fafafa"/>
        
        <View 
            android:layout_width="match_parent"
            android:layout_height="0.66700006dip"
            android:background="#dfdfdf"/>
        
        <TextView 
            android:id="@+id/dialog_delete_bt"
            android:layout_width="match_parent"
            android:layout_height="52dip"
            android:gravity="center"
            android:text="@string/delete"
            android:textColor="#333333"
            android:textSize="14sp"
            android:background="@drawable/dialog_button_bg"/>
        
        <View 
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_gravity="center"
            android:background="#dddddd"/>
        
        <TextView 
            android:id="@+id/dialog_cancel_bt"
            android:layout_width="match_parent"
            android:layout_height="52dip"
            android:gravity="center"
            android:text="@string/cancel"
            android:textColor="#333333"
            android:enabled="true"
            android:textSize="14sp"
            android:background="@drawable/dialog_button_bg"/>
        
    
    </LinearLayout>
    

  • 然后设置该layout布局的style
    <style name="delete_photos_dialog_style">
        <item name="android:windowNoTitle">true</item>
        <item anme="android:backgroundDimEnabled">true</item>
    </style>

  • backgroundDimEnabled属性是是否允许对话框的背景变暗,为ture则背景变暗
  • 建立一个类用来加载该布局,并且调整布局在屏幕上的位置,类的位置vendor/mdiatek/proprietary/packages/apps/Gallery2Zuk/src/com/android/gallery3d/wind/base/view/DeletePhotosDialog.java
    import android.view.Gravity;
    import android.view.View;
    import android.view.LayoutInflater;
    import android.view.Window;
    import android.view.WindowManager;
    import android.content.Context;
    import android.app.Dialog;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    import android.net.Uri;
    import android.os.Bundle;
    
    import com.android.gallery3d.R;
    import com.android.gallery3d.wind.menu.MenuExecutor;
    public class DeletePhotosDialog implements View.OnClickListner{
    	
    	private Context mContext;
    	private Dialog mDialog;
    	private TextView dialogMessage;
    	private TextView dialogDeleteBT;
    	private TextView dialogCancelBT;
    	
    	private ArrayList<Uri> uris;
    	private FragmentCallback callBack;//FragmentCallback应该是MenuExecutor中的一个接口
    	private int id;
    	
    	public DeletePhotosDialog(Context context,ArrayList<Uri> uris,FragmentCallback callBack,int id){
    		mContext=context;
    		this.uris=uris;
    		this.callBack=callBack;
    		this.id=id;
    	}
    	
    	public Dialog createDeletePhotosDialog(String message){
    		mDialog = new Dialog(mContext,R.style.delete_photos_dialog_style);//根据style创建一个dialog
    		LinearLayout root=(LinearLayout)LayoutInflator.from(mContext).inflate(
    				R.layout.delete_photos_dialog_layout,null);
    		dialogMessage=(TextView)root.findViewById(R.id.delete_photos_dialog_message);
    		dialogMessage.setText(message);
    		dialogDeleteBT=(TextView)root.findViewById(R.id.dialog_cancel_bt);
    		
    		dialogDeleteBT.setOnClickListener(this);
    		
    		dialogCancelBT=(TextView)root.findViewById(R.id.dialog_cancel_bt);
    		dialogCancelBT.setOnClickListener(this);
    		mDialog.setContentView(root);//给Dialog对象设置布局
    		Window dialogWindow=mDialog.getWindow();//定义一个Window对象,将mDialog通过getWindow得到的Window赋值给该对象
    		dialogWindow.setGravity(Gravity.BOTTOM);//设置Window的对齐方式为底部对齐
    		WindowManager.LayoutParams lp=dialogWindow.getAttributes();//获取对话框当前的参数值
    		lp.x=0;//新位置X坐标
    		lp.y=130;//新位置Y坐标
    		lp.width=(int)mContext.getResources().getDisplayMetrics().widthPixels;//宽度
    		root.measure(0, 0);//对root布局进行测量
    		lp.height=root.getMeasuredHeight();//得到布局的高度
    		lp.alpha=9f;//透明度
    		dialogWindow.setAttributes(lp);
    		return mDialog;
    		
    	}
    	
    	@Override
    	public void onClick(View v){
    		
    		switch(v.getId()){
    		case R.id.dialog_delete_bt:
    			Bundle bundle = new Bundle();
    			bundle.putParcelableArrayList("URI_LIST", uris);
    			callBack.startMemuAction(id,bundle);
    			if(mDialog!=null){
    				mDialog.dismiss();//点击删除按钮以后,dialog去掉
    			}
    			break;
    		case R.id.dialog_cancel_bt:
    			if(mDialog!=null){
    				mDialog.dismiss();//点击删除按钮以后,dialog去掉
    			}
    			break;
    		default:
    			break;
    		}
    	}
    }
    

  • 参考链接http://blog.csdn.net/fancylovejava/article/details/21617553
  • 找到点击弹出该对话框的按钮,位置在vendor/mdiatek/proprietary/packages/apps/Gallery2Zuk/src/com/android/gallery3d/wind/moment/MomentFragment.java
    public void bottomButtonClick(){
    ......
    ImageView deletePhotos=(ImageView)getActivity().findViewById(R.id.btn_delete);
    deletePhotos.setOnClickListner(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        String confirmMsg="";
        String type=getSelectedItemType();//得到选中项的数据类型
        if(type.equalsIgnoreCase("photo")){
        confirmMsg=getResources().getQuantityString(R.plurals.delete_selection_photo,getSelectedCount(),getSelectedCount());//getSelectedCount()方法获得被选中项的数量
    }else if (type.equalsIgnoreCase("video")){
        confirmMsg=getResources().getQuantityString(R.plurals.delete_selection_video,getSelectedCount(),getSelectedCount());
    }else if(type.equalsIgnoreCase("burst")){
        confirmMsg=getResources().getQuantityString(R.plurals.delete_selection_burst,getSelectedCount(),mConshotCount,getSelectedCount());
    }else{
        confirmMsg=getResources().getQuantityString(R.plurals.delete_selection_item,getSelectedCount(),getSelectedCount());
    }
    mMenuExecutor.onDelete(R.id.btn_delete,confirmMsg,getSelectedUris());//getSelectedUris()方法获取所有被选项的uri
    }
    });
    }

  • 在string.xml中添加如下内容
    <plurals name="delete_selection_photo">
        <item quantity="one">Delete this photo?</item>
        <item quantity="other">Delete %1$d photo(s)?</item>
    </plurals>
    <plurals name="delete_selection_video">
        <item quantity="one">Delete this video?This will also remove \n this video from other albums.</item>
        <iten quantity="other">Delete %1$d video(s)?This will also remove the \n video(s) from other albums.</item>
    </plurals>
    <plurals name="delete_selection_burst">
        <item quantity="one">Delete %1$d photos in this burst sequence?</item>
        <iten quantity="other">Delete %1$d photos in %2$d burst sequences?</item>
    </plurals>
    <plurals name="delete_selection_item">
        <item quantity="one">Delete this item?This will also remove \n this item from other albums.</item>
        <iten quantity="other">Delete %1$d item(s)?This will also remove the \n item(s) from other albums.</item>
    </plurals>

  • 参考链接http://www.cnblogs.com/meiyitian/articles/2221742.html
  • 我们看一实现删除操作的方法onDelete,它在vendor/mdiatek/proprietary/packages/apps/Gallery2Zuk/src/com/android/gallery3d/wind/menu/MenuExecutor.java中,内容如下
    public void onDelete(int id,String confirmMsg,ArrayList<Uri>uriList){
        mUriList=uriLIst;
        if(confirmMsg!=null){
            DeletePhotosDialog deletePhotosDialog=new DeletePhotosDialog(mContext,mUriList,mFragmentCallback,id);//这里调用了我们前面定义的用来加载删除dialog的类DeletePhotosDialog 
            deletePhotosDialog.createDeletePhotosDialog(confirmMsg).show();//这里创建删除dialog并显示
            }else{
                Bundle bundle=new Bundle();
                bundle.putParcelableArrayList("URI_LIST",mUriList);
                mFragmentCallback.startMenuAction(id,bundle);
            }
        }

  • 这里继续补充接口回调的知识
  • 在DeletePhotosDialog.java中点击删除后的操作
    callBack.startMenuAction(id,bundle);

    callBack是接口FragmentCallback的对象,这里是通过DeletePhotosDialog的构造方法传入的
    public DeletePhotosDialog(Context context,ArrayList<Uri> uris,FragmentCallback callBack,int id)

    于是我们去找实际调用DeletePhotosDialog类并传入参数的位置,就是上面提到的onDelete方法,通过该方法体,我们发现mFragmentCallback来自于类MenuExecutor中的构造方法
    public MenuExecutor(Context context,final FragmentCallback fragmentCallback)

    于是我们要去找实际调用MenuExecutor类并传入参数的位置,于是我们发现在类MomentFragment的initViews方法中实际调用了该类
    mMenuExecutor=new MenuExecutor(getActivity(),this);

    这里重点来了,说明MenuExecutor这个类实现了接口FragmentCallback,于是找到实现该类需要重写的方法startMenuAction中的内容,如下
    final MenuFragment menuFragment=MenuFragment.newInstance(actionId,uris);    menuFragment.setProgressCallback(this);

    这里又将类本身传入作为参数,于是我们发现该类同时实现了接口MenuFragment.ProgressCallBack,我们在去看看实现这个接口,都需要重写哪些方法,接口的内容如下
    public interface ProgressCallBack{	
    void onProgressStart(int actionId,int max);	
    void onProgressUpdate(int index);	
    void onProgressComplete(String msg);
    }

    于是我们回到MomentFragment类中,看看该类是如何重写这些方法的,我们发现在重写的方法onProgressStart中,调用了方法showDialog(deletingCount);,于是我们找到该类中的showDialog方法,这个方法就是真正生成ProgressDialog的位置,内容如下
  • private void showDialog(int max){
        int dialogTitle;
        if(actionId==R.id.btn_delete){
            dialogTitle=R.id.btn_delete;
        }else{
             dialogTitle=R.string.ok;
        }
        if(progressDialog !=null && progressDialog.isShowing()){
            progressDialog.cancel();
        }
        progressDialog=CustomProgressDialog.createProgressDialog(getActivity(),R.style.CustomProgressDialog,dialogTitle,max);
    progressDialog.show();
    }

作者:u012966861 发表于2016/11/24 17:49:00 原文链接
阅读:68 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>