Android 开发记录之UI篇(持续更新)

10 篇文章 0 订阅

Android 开发记录之UI篇(持续更新)

  1.如何让自定义的dialog全屏显示:
   在你dialog.show();后面加上
  android.view.WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
  layoutParams.gravity=Gravity.CENTER;
  layoutParams.width= LayoutParams.MATCH_PARENT;
  layoutParams.height= LayoutParams.WRAP_CONTENT;
  getWindow().getDecorView().setPadding(0, 0, 0, 0);
  getWindow().setAttributes(layoutParams);
   或者在创建Dailog的时候指定参数:
  dialog = new Dialog(mContext, R.style.CustomDialog);
  View view = View.inflate(mContext, R.layout.dialog, null); 
  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  dialog.addContentView(view, new android.view.ViewGroup.LayoutParams.LayoutParams((dm - 20), android.view.ViewGroup.LayoutParams.LayoutParams.MATCH_PARENT));
 dialog.show(); 
 注意:这里dialog自定义view,有两种方当时,1.通过build.setView();这种方式只会更改Title和Button之间的内容,上下均会有自带的黑色边框和背景。2.通过dialog.setContentView();这种方式会设置整个内容,但需要在show()方法之后调用;
   
  2.去除dialog的黑色背景:
   <style name="CustomDialog" parent="@android:style/Theme.Dialog">  
    <item name="android:windowNoTitle">true</item>  
    <item name="android:windowBackground">@android:color/transparent</item>  
<item name="android:windowFrame">@null</item> 
   
<item name="android:windowIsFloating">true</item>  
<item name="android:windowContentOverlay">@null</item> 
   </style>

 3.关于dialog的自定义样式:
  在4.0之前,AlertDialog.Build()的构造方法是不支持传自定义的Theme的,4.0以后才支持,但是Dialog的构造方法是支持传递Theme的,那么4.0之前我们可以自定义Dialog的子类,传递Theme,或者使用:AlertDialog.Builder build = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog));
  
 4.关于 ProgressDialog 样式:
  ProgressDialog pDialog = new ProgressDialog(context, ProgressDialog. THEME_HOLO_LIGHT );

 5.反射控制对话框点击按钮后消失或不消失:
   
AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
builder.setTitle(getString(R.string.device_pawd_new))
.setNegativeButton(getString(R.string.dialog_btn_cancel), new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			//对话框消失
			try  {
				Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
				field.setAccessible( true );
				// 将mShowing变量设为true,再去调用dismiss()对话框消失
				field.set(dialog, true );
				dialog.dismiss();
			}catch (Exception e){
				e.printStackTrace();
			}
		}
	});
builder.setPositiveButton(getString(R.string.dialog_btn_confirm), new DialogInterface.OnClickListener() {
	@Override
	public void onClick(DialogInterface dialog, int which) {
			//对话框不消失
			try {
				Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
				field.setAccessible( true );
				// 将mShowing变量设为false,再去调用dismiss()对话框不消失
				field.set(dialog, false );
				dialog.dismiss();
			}catch (Exception e){
				e.printStackTrace();
			}
    }
});

 6 .setBackgroundDrawable 与 setBackgroundColor:

   设置背景图片,图片来源于drawable;

   flightInfoPanel.setBackgroundDrawable(getResources().getDrawable(R.drawable.search_label_click));

   转换字符串为int(颜色);

   listItemView.deleteFilghtBg.setBackgroundColor(Color.parseColor("#F5F5DC"));


 7.代码动态更改RadioButton的图片:

  第一个方法:

setCompoundDrawablesWithIntrinsicBounds(Drawable left,Drawable top,Drawable right,Drawable bottom)

可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。图标的宽高将会设置为固有宽高,既自动通过getIntrinsicWidth和getIntrinsicHeight获取。

1                 button = (RadioButton) group.getChildAt(i);
2                 Resources res = TabTest.this.getResources();
3                 Drawable myImage = res.getDrawable(R.drawable.home);
4                 button.setCompoundDrawablesWithIntrinsicBounds(null, myImage, null, null);

第二种方法:setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。但是Drawable必须已经setBounds(Rect)。意思是你要添加的资源必须已经设置过初始位置、宽和高等信息。这个方法的好处就是不按比例,宽高可以打破原有的大小及比例

1                 Resources res = TabTest.this.getResources();
2                 Drawable myImage = res.getDrawable(R.drawable.home);
3                 myImage.setBounds(1, 1, 100, 100);
4                 button.setCompoundDrawables(null, myImage, null, null);

总结:

radiobutton设置不同方位的图标的方法有以上两种,如果想手动设置大小的话就要用setCompoundDrawables,事先要给Drawable设置setBounds。如果按照原有比例大小显示图片就使用setCompoundDrawablesWithIntrinsicBounds


 8.上拉刷新下拉加载的通用View:走你


 9.自定义View合集:自定义View


 10.系统自带 TimePicker,DatePicker: 
  默认主题的样式不太符合大众审美,只需更改其所在Activity的主题样式即可实现:
  在配置文件中所在activity标签下设置:android:theme="@android:style/Theme.Holo.Light"
  在onCreate的setContentView()之前加上:requestWindowFeature(Window.FEATURE_NO_TITLE);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值