Android 知识点之杂货篇

有时工作中需要一些知识点,就慢慢自己积累起来,空闲时间看看,对某些属性的理解会更深一些

1.隐藏标题全屏展示



requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题  
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
2.获取屏幕密度的方法
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
int W = mDisplayMetrics.widthPixels;
int H = mDisplayMetrics.heightPixels;

// 获取屏幕密度(方法1)  
int screenWidth1  = getActivity().getWindowManager().getDefaultDisplay().getWidth();       // 屏幕宽(像素,如:480px)  
int screenHeight1 = getActivity().getWindowManager().getDefaultDisplay().getHeight();      // 屏幕高(像素,如:800p)

// 获取屏幕密度(方法2)  
DisplayMetrics dm2 = getResources().getDisplayMetrics();  
 
float density  = dm2.density;        // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)  
int densityDPI = dm2.densityDpi;     // 屏幕密度(每寸像素:120/160/240/320)  
float xdpi = dm2.xdpi;             
float ydpi = dm2.ydpi;  
 
int screenWidth2  = dm2.widthPixels;      // 屏幕宽(像素,如:480px)  
int screenHeight2 = dm2.heightPixels;     // 屏幕高(像素,如:800px) 



// 获取屏幕密度(方法3)  
DisplayMetrics dm3 = new DisplayMetrics();  
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm3);  
 
density  = dm3.density;      // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)  
densityDPI = dm3.densityDpi;     // 屏幕密度(每寸像素:120/160/240/320)  
xdpi = dm3.xdpi;           
ydpi = dm3.ydpi;  
 
int screenWidth3 = dm3.widthPixels;          // 屏幕宽(px,如:480px) 
int screenHeight3 = dm3.heightPixels;        // 屏幕高(px,如:800px)  
 
float screenWidthDip  = (dm3.widthPixels/density);    // 屏幕宽(dip,如:320dip)  
float screenHeightDip = (dm3.heightPixels/density);   // 屏幕宽(dip,如:533dip) 




2.设置控件属性
TextView tv=new TextView(context);
LayoutParams layoutParams = tv.getLayoutParams();
    layoutParams.width = 60;
    layoutParams.height = 30;
    tv.setLayoutParams(layoutParams);

4.去除ActionBar的方法
1.//如果想要隐藏上面的ActionBar,可以使用如下的代码:
getActionBar().hide();
//上面的代码会将整个ActionBar都隐藏掉,包括ActionBar中的Tab分页标签,如果想要保留分页标签的话,可以使用如下的代码: 
/*ActionBar actionBar=getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);*/
2.另外还有一种更简单的方式来移除ActionBar,在setContent之前调用下面这句,保证没有ActionBar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
5.ListView和GridView滑动到顶部和底部时出现的阴影消除方法:
android2.3以前用android:fadingEdge="none"
android2.3以后用setOverScrollMode(View.OVER_SCROLL_NEVER)
6.避免启动app时闪一下黑屏或其他颜色问题,在Theme中添加这两句,就可以解决了
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>


7.Theme 属性详解




<!-- The default theme for apps on API level 10 and lower. This is the theme used for
         activities that have not explicitly set their own theme.
         <p>You can count on this being a dark
         background with light text on top, but should try to make no
         other assumptions about its appearance. In particular, the text
         inside of widgets using this theme may be completely different,
         with the widget container being a light color and the text on top
         of it a dark color.
         <p>If you're developing for API level 11 and higher, you should instead use {@link
         #Theme_Holo} or {@link #Theme_DeviceDefault}.</p>
     
android:theme="@android:style/Theme” //背景黑色
android:theme="@android:style/Theme.Black" //背景黑色
android:theme="@android:style/Theme.Black.NoTitleBar"//黑色背景,无标题栏
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" //黑色背景,无标题栏,全屏
android:theme="@android:style/Theme.DeviceDefault"//背景黑色,标题栏黑色
android:theme="@android:style/Theme.DeviceDefault.Dialog//默认主题, Activity显示为对话框模式
android:theme="@android:style/Theme.DeviceDefault.Dialog.Alert"//默认主题, Activity显示为对话框消息模式
android:theme="@android:style/Theme.DeviceDefault.Dialog.MinWidth"//Activity显示为对话框并且加上了最小宽度
android:theme="@android:style/Theme.DeviceDefault.Dialog.NoActionBar" //Activity显示为对话框并且没有标题栏 
android:theme="@android:style/Theme.DeviceDefault.Dialog.NoActionBar.MinWidth" //Activity显示为对话框并且没有标题栏加上了最小宽度 
android:theme="@android:style/Theme.DeviceDefault.DialogWhenLarge"  //一直在想它跟Theme.DeviceDefault的区别..
android:theme="@android:style/Theme.DeviceDefault.DialogWhenLarge.NoActionBar" //没有标题栏 
android:theme="@android:style/Theme.DeviceDefault.InputMethod"  
android:theme="@android:style/Theme.DeviceDefault.Light"//背景白色并且标题栏的颜色也变成了白色
android:theme="@android:style/Theme.DeviceDefault.Light.DarkActionBar" //背景白色标题栏的颜色黑色
android:theme="@android:style/Theme.DeviceDefault.Light.Dialog" //背景白色对话框 
android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.Alert" //背景白色对话框 
android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.MinWidth"//变为一个白色对话框并且加上了最小宽度
android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar" //背景白色对话框没有标题栏 
android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar.MinWidth"//背景白色对话框没有标题栏加上最小宽度 
android:theme="@android:style/Theme.DeviceDefault.Light.DialogWhenLarge" 告诉我它跟DeviceDefault.Light的区别
android:theme="@android:style/Theme.DeviceDefault.Light.DialogWhenLarge.NoActionBar"//没有标题栏 
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar" //背景白色没有标题栏 
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen" //背景白色没有标题栏并且充满屏幕 
android:theme="@android:style/Theme.DeviceDefault.Light.Panel" //本页面中间显示 
android:theme="@android:style/Theme.DeviceDefault.Panel"//本页面中间显示 
android:theme="@android:style/Theme.DeviceDefault.Wallpaper"//背景和手机主界面一样 
android:theme="@android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar"//背景和手机主界面一样没有标题栏 
android:theme="@android:style/Theme.Dialog" //对话框显示
android:theme="@android:style/Theme.Dialog" //Activity显示为对话框模式
android:theme="@android:style/Theme.Holo" //标题栏不一样了.
android:theme="@android:style/Theme.Holo.Dialog" //对话框了 
android:theme="@android:style/Theme.Holo.Dialog.MinWidth" //对话框加最小宽度 
android:theme="@android:style/Theme.Holo.Dialog.NoActionBar" //没有标题栏 
android:theme="@android:style/Theme.Holo.Dialog.NoActionBar.MinWidth" //没有标题栏le加最小宽度 
android:theme="@android:style/Theme.Holo.DialogWhenLarge" 
android:theme="@android:style/Theme.Holo.DialogWhenLarge.NoActionBar"
android:theme="@android:style/Theme.Holo.Light" 
android:theme="@android:style/Theme.Holo.Light.DarkActionBar" //白色注意标题栏  
android:theme="@android:style/Theme.Holo.Light.Dialog"//白色对话框 
android:theme="@android:style/Theme.Holo.Light.Dialog.MinWidth"//白色对话框最小宽度 
android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar"//白色对话框没有标题栏 
android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar.MinWidth"//白色对话框没有标题栏加最小宽度 
android:theme="@android:style/Theme.Holo.Light.DialogWhenLarge"
android:theme="@android:style/Theme.Holo.Light.DialogWhenLarge.NoActionBar" 
android:theme="@android:style/Theme.Holo.Light.NoActionBar"//白色背景没有标题栏  
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen //白色背景没有标题栏且充满屏幕 
android:theme="@android:style/Theme.Holo.Light.Panel"//页面中间显示 
android:theme="@android:style/Theme.Holo.NoActionBar"//没有标题栏 
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" //没有标题栏充满屏幕 
android:theme="@android:style/Theme.Holo.Panel"//页面中间显示 注意颜色和Theme.Holo.Light.Panel的区别 
android:theme="@android:style/Theme.Holo.Wallpaper" //背景和手机主界面一样  
android:theme="@android:style/Theme.Holo.Wallpaper.NoTitleBar"//背景和手机主界面一样、没有标题栏 
android:theme="@android:style/Theme.InputMethod" 
android:theme="@android:style/Theme.Leanback.FormWizard"//字体变化了 
android:theme="@android:style/Theme.Light"//白色背景
android:theme="@android:style/Theme.Light.NoTitleBar"//白色背景没有标题栏 
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"//白色背景没有标题栏、充满屏幕 
android:theme="@android:style/Theme.Light.Panel" //页面中间显示且背景和手机主界面一样 
android:theme="@android:style/Theme.Light.WallpaperSettings" //背景和主界面背景一样但虚化了  
android:theme="@android:style/Theme.NoDisplay"//不显示了
android:theme="@android:style/Theme.NoTitleBar"//没有标题栏 
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"//没有标题栏、充满屏幕 
android:theme="@android:style/Theme.NoTitleBar.OverlayActionModes"
android:theme="@android:style/Theme.Panel"//本页面中间显示 
android:theme="@android:style/Theme.Translucent" //透明背景
android:theme="@android:style/Theme.Translucent.NoTitleBar" //透明背景,无标题栏
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" //透明背景,无标题栏,全屏
android:theme="@android:style/Theme.Wallpaper" //用系统桌面为应用程序背景
android:theme="@android:style/Theme.Wallpaper.NoTitleBar" //用系统桌面为应用程序背景,且无标题栏
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" //用系统桌面为应用程序背景,无标题栏,全屏
android:theme="@android:style/Theme.WithActionBar"//标题栏 
很多主题在使用时会报错,原因有很多,比如窗体必须继承AppCompactActivity,或者要继承ActionBarActiivty,或者要继承FragmentActivity,或者需要手动指定宽高,或者需要提升最低API版本,或者需要更高版本的SDK,或者兼容包版本不对等原因。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值