[转帖] Android资源文件使用经验_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3405 | 回复: 0   主题: [转帖] Android资源文件使用经验        下一篇 
周逸涵
注册用户
等级:少校
经验:871
发帖:83
精华:0
注册:2013-7-8
状态:离线
发送短消息息给周逸涵 加好友    发送短消息息给周逸涵 发消息
发表于: IP:您无权察看 2013-7-9 10:15:37 | [全部帖] [楼主帖] 楼主

5.关于尺寸单位

     Android默认160dots per inch (在屏幕dpi为160的时候,1 dip == 1 px)
    有的手机是120 per inch, density的值为120/160=0.75(此时1 dip == 0.75 px)
    有的手机是240 per inch, density的值为240/160=1.5(此时1 dip == 1.5 px)
    编辑资源文件时候view的尺寸一定用dip,字体用sp

 dips * density = pixels


    必须直接指定的话,可通过dip转换,如下:

 final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);


17.一个LinearLayout中有一个ImageView和一个TextView,点击更换背景的xml文件中只设置LinearLayout的背景,点击没有效果

[html]view plaincopyprint?

  1. <LinearLayout  
  2.             android:layout_width="fill_parent"  
  3.             android:layout_height="wrap_content"  
  4.             android:layout_weight="1"  
  5.             android:background="@drawable/list_selector"  
  6.             android:gravity="center_horizontal"  
  7.             android:orientation="vertical"  
  8.             android >  
  9.             <ImageView  
  10.                 android:id="@+id/iv_animation"  
  11.                 android:layout_width="wrap_content"  
  12.                 android:layout_height="wrap_content"  
  13.                 android:background="#bb328EAE"  
  14.                 android:contentDescription="@string/app_name"  
  15.                 android:padding="@dimen/normal_padding"  
  16.                 android:src="@drawable/ic_launcher" />  
  17.             <TextView  
  18.                 android:layout_width="wrap_content"  
  19.                 android:layout_height="wrap_content"  
  20.                 android:text="@string/AsyncTask"  
  21.                 android:textColor="#ff0686b5"  
  22.                 android:textSize="14.0sp" />  
  23.         </LinearLayout>  


16.Rotate的xml文件编写方法。动画资源文件中的单位

[html]view plaincopyprint?

  1. <rotate android:fromDegrees="0"  
  2. android:toDegrees="+350"  
  3. android:pivotX="50%"  
  4. android:pivotY="50%"  
  5. android:duration="3000"/>  
  6. androidpivotX的值共有三红设置方法:  
  7. 1.android:pivotX="50"这种方法使用绝对位置定位;  
  8. 2.android:pivotX="50%"这种方法相对于控件本身定位;  
  9. 3.android:pivotX="50%p"这种方法相对于控件的父控件定位;  

15.color资源的使用注意

在color中定义的color资源#AARRGGBB的颜色,

在代码中set颜色的时候直接写R.color.*和

getResources().getColor(R.color.*);的效果是不一样的。务必注意

14.逐帧动画资源文件模板

[html]view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/selected" android:oneshot="false" >  
  4.     <item android:drawable="@drawable/horse1" android:duration="300"/>  
  5.     <item android:drawable="@drawable/horse2" android:duration="300"/>  
  6.     <item android:drawable="@drawable/horse3" android:duration="300"/>  
  7.     <item android:drawable="@drawable/horse4" android:duration="300"/>  
  8.     <item android:drawable="@drawable/horse5" android:duration="300"/>  
  9.     <item android:drawable="@drawable/horse6" android:duration="300"/>  
  10. </animation-list>  


13.java命令行编译多个源文件

javac -d . Example1.java Example2.java                             ; Example1是主类,要用到Exmaple2
执行:java com.example.Example1                                    ; 执行时要加上完整包名

13.1命令行编译使用外部jar包

javac -cp jdom.jar -d . IFlow.java NetworkUtil.java


执行

java -classpath jdom.jar; com.bu.qs.IFlow     ;注意空格和分号;

12.WebView load html文档

mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
mWebView.loadData(it.getStringExtra(CONTENT), "text/html", "UTF-8");


11.ListView 中item的起始位置

当没有设置header的时候是从0开始;设置headerView的情况下,item是从1开始的。

10.TextView添加下划线的方法

①在代码中实现

mLinkText.setText(Html.fromHtml("<u>"+mLinkUrl+"</u>"));


②在资源文件中

<string name="helloworld"><u>yesorno</u></string>


9.省资源的设置背景方法

[html]view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <bitmap android:src="@drawable/bg" android:tileMode="repeat"  
  3.   xmlns:android="http://schemas.android.com/apk/res/android" />  


bg为一个非常小的图片,tileMode重复绘制。

8.常用的一些常量

View.GONE,VISIBLE,INVISIBLE


ScaleType的常量如7.

LayoutParams的常量ViewGroup.LayoutParams.FILL_PARENT,MATCH_PARENT,WRAP_CONTENT

8.3 ActionBar的常量NAVIGATION_MODE_LIST弹出菜单,NAVIGATION_MODE_STANDARD,NAVIGATION_MODE_TABS选项卡

8.4MenuItem的常量

SHOW_AS_ACTION_ALWAYS
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
SHOW_AS_ACTION_IF_ROOM
SHOW_AS_ACTION_NEVER
SHOW_AS_ACTION_WITH_TEXT


7.ImageVew的ScaleType属性

从网络上获取的图片尺寸大小不一,可以设置这个属性,保持一样大小。这是一个枚举型常量可以取值:

CENTER, CENTER_CROP, CENTER_INSIDE, FIT_CENTER, FIT_END, FIT_START, FIT_XY, MATRIX


iv.setScaleType(ImageView.ScaleType.FIT_XY);即可让图片适应宽高一样大了,不过有拉伸变形。

具体参考http://hi.baidu.com/kirahai/item/b49ebf5e53eeb63733e0a9f9

8.ViewPager和一个进度条模板


[html]view plaincopyprint?

  1. <RelativeLayout  
  2.         android:layout_width="wrap_content"  
  3.         android:layout_height="fill_parent" >  
  4.         <ProgressBar  
  5.             android:id="@+id/pb_loading"  
  6.             android:layout_width="wrap_content"  
  7.             android:layout_height="wrap_content"  
  8.             android:layout_centerInParent="true" />  
  9.         <android.support.v4.view.ViewPager  
  10.             android:id="@+id/vp_servicetab"  
  11.             android:layout_width="wrap_content"  
  12.             android:layout_height="fill_parent"  
  13.             android:layout_gravity="center"  
  14.             android:layout_weight="1.0"  
  15.             android:background="#000000"  
  16.             android:flipInterval="30"  
  17.             android:persistentDrawingCache="animation"  
  18.             android:visibility="invisible" />  
  19.     </RelativeLayout>  



[html]view plaincopyprint?

  1. <FrameLayout  
  2.         android:layout_width="wrap_content"  
  3.         android:layout_height="fill_parent" >  
  4.         <android.support.v4.view.ViewPager  
  5.             android:id="@+id/vp_servicetab"  
  6.             android:layout_width="wrap_content"  
  7.             android:layout_height="fill_parent"  
  8.             android:layout_gravity="center"  
  9.             android:layout_weight="1.0"  
  10.             android:background="#000000"  
  11.             android:flipInterval="30"  
  12.             android:persistentDrawingCache="animation"  
  13.              />  
  14.         <ProgressBar  
  15.             android:id="@+id/pb_loading"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_gravity="center"  
  19.              />  
  20.     </FrameLayout>  



6.TextView实现文本内容滚动

6.1用ScrollView或者HorizontalScrollView嵌套即可;前者实现垂直滚动,后者实现水平滚动  

6.2如果想让TextView的文本无论多长都只有1行的话 singleLine为true即可,用ellipsize属性可以出现一个省略号,表示该TextView还有更多的内容,此属性可以取的值有start,middle,end,none,marquee,最后一个效果为走马灯。

最后一个效果试了试,不起作用,马灯没走,如下设置才会走马灯,那个marqueeRepeatLimit是个int值,如下就是不停的走,那个水平滚动为默认,不设置也可以。手机Android2.3测试OK

[html]view plaincopyprint?

  1. <TextView android:layout_width="100px"  
  2.         android:layout_height="wrap_content"  
  3.         android:textColor="@android:color/white"   
  4.         android:ellipsize="marquee"   
  5.         android:focusable="true"   
  6.         android:marqueeRepeatLimit="marquee_forever"   
  7.         android:focusableInTouchMode="true"   
  8.         android:scrollHorizontally="true"  
  9.         android:text="she is the one that you never forget she is the heaven sent angel you met."  
  10.         >  
  11.     </TextView>  



1.ListView的使用

android:cacheColorHint="ffffffff"或者listView.setCacheColorHint(Color.alpha(255));//按住时候没有阴影

android:divider="#255"//分割线透明


android:fadingedge="none"边缘无阴影

代码相关:(此处为引用他人博客,向作者致谢)

在ListView里,HeaderView和FooterView也占一行,与其他的item一样,可以点击,有索引,HeaderView的索引为0.如果要使这两项不可点击,可以使用下面的方法:

publicvoid addFooterView(View v,Object data, boolean isSelectable)
publicvoid addHeaderView(View v,Object data, boolean isSelectable)


如果在view里已经填充数据,第二个参数可以为空,第三个参数设为false,即不可选择

通用属性:

android:hint="提示:请输入用户名"         //edittext常用
android:visibility="gone"或者setVisibility(View.GONE) //View.GONE 一个不占空间,彻底没了。
android:visibility="invisible"或者setVisibility(View.INVISIBLE) //View.INVISIBLE 区别是一个占空间,只是看不见而已

总结:

(1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)

(2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x480)

(3)drawable-ldpi里面存放低分辨率的图片,如QVGA (240x320)

有layout的属性是本控件在容器中的位置 没有layout的属性是控件中的内容的位置

Strings.xml中关于使用多个空格的方法:&#160;        //想要多个空格就多加不要忘记分号

2.关于透明度

颜色和不透明度 (alpha) 值以十六进制表示法表示。

任何一种颜色的值范围都是 0 到 255(00 到 ff)。

对于 alpha,00 表示完全透明,ff 表示完全不透明。

表达式顺序是“aabbggrr”,其中“aa=alpha”(00 到 ff);“bb=blue”(00 到 ff);“gg=green”(00 到 ff);“rr=red”(00 到 ff)。

例如,如果您希望对某叠加层应用不透明度为 50% 的蓝色,则应指定以下值:7fff0000

全透明:指定控件的android:background属性为"@android:color/transparent"

alpha值:android:background="#00000000"

3.LinearLayout中的疑问

[html]view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <!-- 线性布局中把一个控件的weight的属性设置为1,即可显示其他控件,什么原理?  
  7.     height为0dp will performance better! -->  
  8.     <ListView  
  9.         android:id="@id/lv_app_recommend"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="0.0dp"  
  12.         android:cacheColorHint="#ffffffff"  
  13.         android:fadingEdge="none"   
  14.         android:layout_weight="1">  
  15.     </ListView>  
  16.     <Button   
  17.         android:id="@id/btn_guess_again"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="@string/guess_again"  
  21.         />  
  22. </LinearLayout>  

4.selector.xml模板

[html]view plaincopyprint




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论