自定义对话框设置style
<style name="TipsDialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowFullscreen">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowBackground">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
<style name="myDialogTheme" parent="android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowNoTitle">true</item><!--除去title--> <item name="android:windowContentOverlay">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground">@null</item><!--除去背景色-->
一般我们将一些公共属性抽象出来做为一个个style,在xml中直接引用style即可,既减少了代码而且在style发生变化的时候一处改变所引用的地方均变化
<style name="layout_match_parent"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">match_parent</item> </style> <style name="layout_wrap_content"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style> <style name="layout_weight_horizontal"> <item name="android:layout_width">0dip</item> <item name="android:layout_height">match_parent</item> </style> <style name="layout_weight_vertical"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">0dip</item> </style>
具体使用方式:例如我们需要自定义一个dialog,不需要dialog的title代码如下:
/** * 图片放大dialog * * @author jone * * 2014年5月13日 com.tiza.tree.xingchang_line.widget * */ public class ImageDialog extends Dialog implements android.view.View.OnClickListener { public ImageDialog(Context context, int theme) { super(context, theme); } ImageView imageView; Button repeatButton; Button cancleButton; View rootView; LayoutInflater inflater; public static final int POSITIVE = 1; public static final int NEGATIVE = 2; public ImageDialog(Context context) { //调用自定义的style,用来去掉dialog默认的title this(context, R.style.MyDialog); inflater = LayoutInflater.from(context); init(); wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; Log.i("tag", screenHeight+":"+screenHeight); } public Button getRightButton(){ return repeatButton; } private void init() { rootView = inflater.inflate(R.layout.xc_img_dialog, null); setContentView(rootView); imageView = (ImageView) rootView.findViewById(R.id.xc_big_img_); repeatButton = (Button) rootView.findViewById(R.id.xc_big_left); cancleButton = (Button) rootView.findViewById(R.id.xc_big_right); repeatButton.setOnClickListener(this); cancleButton.setOnClickListener(this); } public void setImageView(BitmapDrawable drawable) { imageView.setImageDrawable(drawable); } public void setDialogLayoutParamtrs(){ LayoutParams params = new LayoutParams(screenWidth-20, screenHeight-200); params.setMargins(10, 25, 10, 10); imageView.setLayoutParams(params); } public interface ImageListener { public void onButtonClick(int id); } public ImageListener listener; private DisplayMetrics dm; private WindowManager wm; private int screenWidth; private int screenHeight; public void setImageListener(ImageListener listener) { this.listener = listener; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.xc_big_left: if (listener != null) { listener.onButtonClick(POSITIVE); } break; case R.id.xc_big_right: if (listener != null) { listener.onButtonClick(NEGATIVE); } break; default: break; } }
常见android中的style
时间: 2024-10-15 11:36:16