首页 文章 精选 留言 我的

精选列表

搜索[UI库],共10000篇文章
优秀的个人博客,低调大师

Android UI学习 - GridView和ImageView的使用

GridView: A view that shows items in two-dimensional scrolling grid. The items in the grid come from the ListAdapterassociated with this view. 简单说GridView就是我们资源管理器平常见到的一个个文件的icon显示方式。 上面提及到了GridView的Item是来自ListAdapter的所以一般在Activity的onCreate使用GridView的代码 @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.grid_2); GridViewg=(GridView)findViewById(R.id.myGrid); g.setAdapter(newImageAdapter(this)); } 而ImageAdapter一般是extends BaseAdapter。BaseAdapter是implements ListAdapter SpinnerAdapter但很多时候自定义的Adapter都是override ListAdapter的父类Adapter接口里面的方法 int getCount() 获取当前Adapter的Items数目 Object getItem(int position) 获取相应position的Item long getItemId(int position) 获取相应position的Item在List中的row id View getView(int position, View convertView, ViewGroup parent) 获取在指定position所要显示的data的View 这些方法函数和swing的差不多都是基于MVC。大概原理过程是这样的程序需要显示GridView那么要把data一个一个地显示出来是通过一个for循环首先call Adapter.getCount()得到有多少个data然后position++地getItemgetView得到要显示的view,这样子逐一地显示出来 下面是官方sample里面的Photo Grid的例子本人省略了某些代码 publicclassImageAdapterextendsBaseAdapter{ publicImageAdapter(Contextc){ mContext=c; } publicintgetCount(){ returnmThumbIds.length; } publicObjectgetItem(intposition){ returnposition; } publiclonggetItemId(intposition){ returnposition; } publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ ImageViewimageView; if(convertView==null){ imageView=newImageView(mContext); imageView.setLayoutParams(newGridView.LayoutParams(45,45));//设置ImageView宽高 imageView.setAdjustViewBounds(false); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8,8,8,8); }else{ imageView=(ImageView)convertView; } imageView.setImageResource(mThumbIds[position]); returnimageView; } privateContextmContext; privateInteger[]mThumbIds={ R.drawable.sample_thumb_0,R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,R.drawable.sample_thumb_7 }; } 留意getView里面的代码要判断convertView是否为null以便重用减少对象的创建减少内存占用。 XML布局文件内容原来就只是指明GridView <?xmlversion="1.0"encoding="utf-8"?> <GridViewxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:columnWidth="60dp" android:stretchMode="columnWidth" android:gravity="center" /> 可以看到getView和ImageView是重点影响图片的显示效果。而且发现列数是不确定的取决于每个ImageView的宽度和屏幕的宽度。接下来看看ImageView。 ImageViewDisplays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting。 ImageView就是用来显示Imageicon的。 这里我们重点理解ImageView的属性android:scaleType即ImageView.setScaleType(ImageView.ScaleType)。android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /android:scaleType值的意义区别 CENTER /center按图片的原来size居中显示当图片长/宽超过View的长/宽则截取图片的居中部分显示 CENTER_CROP /centerCrop按比例扩大图片的size居中显示使得图片长(宽)等于或大于View的长(宽) CENTER_INSIDE /centerInside将图片的内容完整居中显示通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽 FIT_CENTER / fitCenter把图片按比例扩大/缩小到View的宽度居中显示 FIT_END /fitEnd 把图片按比例扩大/缩小到View的宽度显示在View的下部分位置 FIT_START /fitStart 把图片按比例扩大/缩小到View的宽度显示在View的上部分位置 FIT_XY /fitXY把图片 不按比例扩大/缩小到View的大小显示 MATRIX / matrix用矩阵来绘制 一开始我不明白MATRIX矩阵网上搜索后发现原来MATRIX矩阵可以动态缩小放大图片来显示这里不展开深入的了解只是贴出相关语句缩小图片 //获得Bitmap的高和宽 intbmpWidth=bmp.getWidth(); intbmpHeight=bmp.getHeight(); //设置缩小比例 doublescale=0.8; //计算出这次要缩小的比例 scaleWidth=(float)(scaleWidth*scale); scaleHeight=(float)(scaleHeight*scale); //产生resize后的Bitmap对象 Matrixmatrix=newMatrix(); matrix.postScale(scaleWidth,scaleHeight); BitmapresizeBmp=Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true); 应用ImageView的例子很多看看上次FrameLayout里面的 <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/candle" /> ** 要注意一点我发现Drawable文件夹里面的图片命名是不能大写的。 本文转自 Icansoft 51CTO博客原文链接http://blog.51cto.com/android/316255

优秀的个人博客,低调大师

Android:UI控件风格与主题、selector、Theme

注:默认设置放在最后。 1.颜色xml:values-->resources-->color.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <resources> <colorname= "yellow" >#FFFF00</color> <colorname= "zise" >#D15FEE</color> <colorname= "lightblue" >#8DB6CD</color> </resources> 2.颜色selector:Color List-->selector-->/color/xxx.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:color= "@color/yellow" /> <itemandroid:color= "@color/zise" /> </selector> 3.尺寸(文件大小、控件大小等)dimens:values-->dimens.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <resources> <!--Defaultscreenmargins,pertheAndroidDesignguidelines.--> <dimenname= "activity_horizontal_margin" >16dp</dimen> <dimenname= "activity_vertical_margin" >16dp</dimen> <dimenname= "btn_head_width" >100dp</dimen> <dimenname= "btn_head_high" >100dp</dimen> <dimenname= "edit_width" >200dp</dimen> <dimenname= "edit_high" >60dp</dimen> <dimenname= "edit_width_xhdpi" >60dp</dimen> <dimenname= "edit_high_xhdpi" >160dp</dimen> <dimenname= "textsize" >20dp</dimen> <dimenname= "EditText_layout_marginTop" >80dp</dimen> <dimenname= "textView_layout_marginTop" >26dp</dimen> </resources> 4.背景图片:Drawable-->selector-->/drawable/xxx.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:drawable= "@drawable/ic_pressed" ></item> <itemandroid:drawable= "@drawable/ic_default" ></item> </selector> 5.主题相关 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 常用的系统主题: 1 .没有标题: @android :style/Theme.Black.NoTitleBar 2 .全屏幕 @android :style/Theme.Black.NoTitleBar.Fullscreen 3 .透明 @android :style/Theme.Translucent 4 .桌面壁纸作为背景 @android :style/Theme.Wallpaper 5 .对话框 @android :style/Theme.Dialog 对Activity的生命周期是否有影响? 练习:第一个Activity中有两个按钮, 一个弹出对话框主题的Activity, 另一个弹出对话框,测试二者对 第一个Activity生命周期是否有影响。 弹出对话框主题的Activity: 暂停状态,onPause() 弹出对话框 生命周期没有影响 使用场景: 1 .对话框主题的Activity 例如:在桌面弹出短信内容对话框 对话框后面的Activity是其他App的,这时必须使用对话框主题的 Activity 2 .对话框 需要使用Activity中的成员变量时 6.设置一按钮点击时图片和文字颜色都有高亮效果 1 2 3 4 5 6 7 8 9 10 11 12 13 <TextView android:id= "@+id/home_tv_city" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentLeft= "true" android:layout_centerVertical= "true" android:drawableLeft= "@drawable/selector_home_citymore" android:drawablePadding= "4dp" android:gravity= "center" android:paddingRight= "12dp" android:text= "厦门" android:textColor= "@color/selector_home_city" android:textSize= "20sp" /> res/drawable/selector_home_citymore.xml代码: 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:drawable= "@drawable/home_morecity_pre" ></item> <itemandroid:drawable= "@drawable/home_morecity_nor" ></item> </selector> res/color/selector_home_city.xml代码: 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:color= "@color/cyan" /> <itemandroid:color= "@color/white" /> </selector> 7.创建一个shape的selector 1 2 3 4 5 6 7 8 9 10 11 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" ><shape> <solidandroid:color= "@color/gray_text" /> </shape></item> <item><shape> <solidandroid:color= "@color/touming" /> </shape></item> </selector> 8.代码编写selector 范例: 布局xml代码: 1 2 3 4 5 6 7 8 <TextView android:id= "@+id/TextView_title" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:focusable= "true" android:drawableTop= "@drawable/selector_tabwidget_icon" android:textAlignment= "center" /> selector的xml代码: 1 2 3 4 5 6 7 8 9 10 11 12 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <!--Nonfocusedstates--> <itemandroid:state_focused= "false" android:state_selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact" /> <itemandroid:state_focused= "false" android:state_selected= "true" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <!--Focusedstates--> <itemandroid:state_focused= "true" android:state_selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <itemandroid:state_focused= "true" android:state_selected= "true" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <!--Pressed--> <itemandroid:state_selected= "true" android:state_pressed= "true" android:drawable= "@drawable/contact_sel" /> <itemandroid:state_pressed= "true" android:drawable= "@drawable/contact_sel" /> </selector> selector的java代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 StateListDrawabledrawable= new StateListDrawable(); //Nonfocusedstates drawable.addState( new int []{-android.R.attr.state_focused,-android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact)); drawable.addState( new int []{-android.R.attr.state_focused,android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); //Focusedstates drawable.addState( new int []{android.R.attr.state_focused,-android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); drawable.addState( new int []{android.R.attr.state_focused,android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); //Pressed drawable.addState( new int []{android.R.attr.state_selected,android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); drawable.addState( new int []{android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); TextViewtextView=(TextView)findViewById(R.id.TextView_title); textView.setCompoundDrawablesWithIntrinsicBounds( null ,drawable, null , null ); 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1208480,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android UI-底部旋转菜单栏

以前都是说每逢佳节倍思亲,现在的工作状态是每到周末倍亲切,年底真的是加班加的没完没了的,也没时间写博客,也没时间学习,周末闲来无事看到一个比较有意思的旋转菜单,没事自己实战了一下感觉还不错,代码倒是没什么,主要是有两个技术点,一个就是布局文件,第二个就是动画旋转,关于布局文件是仁者见仁智者见智,只能自己研究,动画的话之前写过这方面的文章有兴趣的可以看下本人之前的博客,开始正题吧: 基础布局 先看下要实现的效果吧: 下面的主要是三个图片,一个半圆,两个版圆环,最外面的是三级菜单,中间的是二级菜单; 一级菜单布局: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <RelativeLayout android:id= "@+id/menuFirst" android:layout_width= "100dp" android:layout_height= "50dp" android:layout_alignParentBottom= "true" android:layout_centerHorizontal= "true" android:background= "@drawable/menu1" > <ImageView android:id= "@+id/icon_home" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerInParent= "true" android:background= "@drawable/icon_home" /> </RelativeLayout> 二级菜单布局: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <RelativeLayout android:id= "@+id/menuSecond" android:layout_width= "170dp" android:layout_height= "90dp" android:layout_alignParentBottom= "true" android:layout_centerHorizontal= "true" android:background= "@drawable/menu2" > <ImageView android:id= "@+id/icon_search" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentBottom= "true" android:layout_margin= "8dp" android:background= "@drawable/icon_search" /> <ImageView android:id= "@+id/icon_menu" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerHorizontal= "true" android:layout_marginTop= "5dp" android:background= "@drawable/icon_menu" /> <ImageView android:id= "@+id/icon_center" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentBottom= "true" android:layout_alignParentRight= "true" android:layout_margin= "8dp" android:background= "@drawable/icon_center" /> </RelativeLayout> 三级菜单布局,这个布局的时候需要注意的是左边的三个图片设置完之后,设置的是对称方向的最底部的一个图片,以此为依据搞定其他两个图标: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 <RelativeLayout android:id= "@+id/menuThird" android:layout_width= "270dp" android:layout_height= "140dp" android:layout_alignParentBottom= "true" android:layout_centerHorizontal= "true" android:background= "@drawable/menu3" > <ImageView android:id= "@+id/channel1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentBottom= "true" android:layout_marginBottom= "8dp" android:layout_marginLeft= "8dp" android:background= "@drawable/channel1" /> <ImageView android:id= "@+id/channel2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_above= "@id/channel1" android:layout_alignLeft= "@id/channel1" android:layout_marginBottom= "8dp" android:layout_marginLeft= "16dp" android:background= "@drawable/channel2" /> <ImageView android:id= "@+id/channel3" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_above= "@id/channel2" android:layout_alignLeft= "@id/channel2" android:layout_marginBottom= "8dp" android:layout_marginLeft= "30dp" android:background= "@drawable/channel3" /> <ImageView android:id= "@+id/channel4" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerHorizontal= "true" android:layout_marginTop= "5dp" android:background= "@drawable/channel4" /> <ImageView android:id= "@+id/channel7" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentBottom= "true" android:layout_alignParentRight= "true" android:layout_marginBottom= "8dp" android:layout_marginRight= "8dp" android:background= "@drawable/channel7" /> <ImageView android:id= "@+id/channel6" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_above= "@id/channel7" android:layout_alignRight= "@id/channel7" android:layout_marginBottom= "8dp" android:layout_marginRight= "20dp" android:background= "@drawable/channel6" /> <ImageView android:id= "@+id/channel5" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_above= "@id/channel6" android:layout_alignRight= "@id/channel6" android:layout_marginBottom= "8dp" android:layout_marginRight= "30dp" android:background= "@drawable/channel5" /> </RelativeLayout> 实现Demo 主要实现的主要就是两个按钮,一个按钮式最底层的按钮,一个是二级菜单的按钮: 1 2 3 4 5 6 7 homeView = (ImageView) findViewById(R.id.icon_home); menuView = (ImageView) findViewById(R.id.icon_menu); menuFirst = (RelativeLayout) findViewById(R.id.menuFirst); menuSecond = (RelativeLayout) findViewById(R.id.menuSecond); menuThird = (RelativeLayout) findViewById(R.id.menuThird); homeView.setOnClickListener( this ); menuView.setOnClickListener( this ); 两个按钮的点击事件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.icon_home: if (isSecond) { MyHelper.StartAninationOut(menuSecond, 500 , 200 ); if (isThird) { MyHelper.StartAninationOut(menuThird, 500 , 300 ); isThird= false ; } } else { MyHelper.StartAninationIn(menuSecond, 500 , 300 ); } isSecond=!isSecond; break ; case R.id.icon_menu: if (isThird) { MyHelper.StartAninationOut(menuThird, 500 , 200 ); isThird= false ; } else { MyHelper.StartAninationIn(menuThird, 500 , 200 ); isThird= true ; } break ; default : break ; } } 两个按钮都有点击点击事件,封装一个可以主要就是淡入和淡出的效果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class MyHelper { public static void StartAninationIn(RelativeLayout layout, long duratoin, long offset) { // TODO Auto-generated method stub RotateAnimation rotateAnimation= new RotateAnimation( 180 , 360 , layout.getWidth()/ 2 , layout.getHeight()); rotateAnimation.setDuration(duratoin); rotateAnimation.setFillAfter( true ); //保持旋转之后的状态 rotateAnimation.setStartOffset(offset); //延迟加载时间 layout.startAnimation(rotateAnimation); } public static void StartAninationOut(RelativeLayout layout, long duratoin, long offset) { // TODO Auto-generated method stub RotateAnimation rotateAnimation= new RotateAnimation( 0 , 180 , layout.getWidth()/ 2 , layout.getHeight()); rotateAnimation.setDuration(duratoin); rotateAnimation.setFillAfter( true ); rotateAnimation.setStartOffset(offset); layout.startAnimation(rotateAnimation); } } 最后看下效果图: 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4162283.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android优化——UI优化(五) Listview 重用convertView

1.重用convertView 我们对convertView添加判断,如果存在我们就直接使用,否则初始化一个convertView的实例。(如下图) 2.使用viewHolder 使用viewHolder并且是一个静态的匿名内部类。(如下图) 3.在列表里面有图片的情况下,监听滑动不加载图片 1.可以查看一下我的这篇文章Listview 2.这个建议用一些图片请求框架,如:Android-Universal-Image-Loader,推荐个中文解析的网站http://codekk.com/ 4.多个不同布局,可以创建不同的viewHolder和convertView进行重用 比如聊天:左边一个布局,右边一个布局,我们可以创建不同的viewHolder,并且对convertView进行判断每个不同的convertView只初始化一次。 本文转自 一点点征服 博客园博客,原文链接: http://www.cnblogs.com/ldq2016/p/5226687.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

如何让Android UI使用更快更高效

之前有谈过如何使用adapter更高效的,现在在谈谈其他的。 一、选择恰当的图像尺寸 视图背景图总是会填充整个视图区域,图像尺寸的不适合会导致图像的自动缩放,为了避免这种情况,我们可以先将图片进行缩放到视图的大小。 originalImage = Bitmap.createScaledBitmap( originalImage, //被缩放图 view.getWidth(), //视图宽度 view.getHright(), //视图高度 true //双限行过滤器 ); 二、去掉不需要的默认窗口背景 在默认情况下,窗口有一个不透明的背景,有时候我们并不需要他,就可以去掉他。因为更新看不见的窗口是浪费时间的。 去掉的方法: 1.代码实现: @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); //删除窗口背景 getWindow().setBackgroundDrawable(null); } 2.xml里实现: 首先去顶你的res/xml/styles.xml里有 <resources> <style name="NoBackGroundTheme" parent="android:Theme"> <item name="android:windowBackground">@null</item> </style> </resources> 然后在你的manifest.xml里声明 <activity android:name="MyActivity" android:theme="@style/NoBackGroundTheme"> ...... </activity> 三、尽可能的使用简单的布局和视图 如果一个窗口包含很多的视图,那么启动时间长、测量时间长、绘制时间长、布局时间长; 如果视图树深度太深,会导致StackOverflowException异常,和用户界面反映会很慢很慢。 解决的方法: 1.使用TextView的复合drawables,减少层次 如有这样的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Image android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" android:background="@drawable/icon" /> </LinearLayout> 我们可以这样来取代他,从而来将少层次: <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:drawableRight="@drawable/icon"/> 2.使用ViewStub延迟展开视图 默认情况下,使用ViewStub包含的视图是不可见的。 <ViewStub android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/vs" android:layout="@layout/main"/> 这个里面包含的main视图是不会展现出来的,如果需要展现出来需要代码的处理 findViewById(R.id.vs).setVisibility(View.VISIBLE); 或者 findViewById(R.id.vs).inflate(); 3.使用合并视图 默认情况下,布局文件的根作为一个借点加入到父视图中,如果使用可以避免根节点。 如果最外层的布局是FrameLayout,那么可以使用merge替换掉,引用官方说明: Obviously, using works in this case because the parent of an activity's content view is always a FrameLayout. You could not apply this trick if your layout was using a LinearLayout as its root tag for instance. <merge xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> ..... </merge> 4.使用RelativeLayout减少层次 5.自定义布局 本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/760990,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android UI学习 - Tab的学习和使用

本文是参考Android官方提供的sample里面的ApiDemos的学习总结。 TabActivity 首先Android里面有个名为TabActivity来给我们方便使用。其中有以下可以关注的函数 public TabHost getTabHost ()获得当前TabActivity的TabHost public TabWidget getTabWidget ()获得当前TabActivity的TabWidget public void setDefaultTab (String tag)这两个函数很易懂就是设置默认的Tab public void setDefaultTab (int index)通过tab名——tag或者index(从0开始) protected void onRestoreInstanceState (Bundle state)这两个函数的介绍可以 protected void onSaveInstanceState (Bundle outState)参考 Activity的生命周期 TabHost 那么我们要用到的Tab载体是TabHost需要从TabActivity.getTabHost获取。 现在看看TabHost类它有3个内嵌类1个类TabHost.TabSpec2个接口TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面会介绍这些类和接口。 TabHost类的一些函数 public void addTab (TabHost.TabSpec tabSpec)添加tab参数TabHost.TabSpec通过下面的函数返回得到 public TabHost.TabSpec newTabSpec (String tag)创建TabHost.TabSpec public void clearAllTabs ()remove所有的Tabs public int getCurrentTab () public String getCurrentTabTag () public View getCurrentTabView () public View getCurrentView () public FrameLayout getTabContentView ()返回Tab content的FrameLayout public TabWidget getTabWidget () public void setCurrentTab (int index) 设置当前的Tab by index public void setCurrentTabByTag (String tag)设置当前的Tab by tag public void setOnTabChangedListener (TabHost.OnTabChangeListener l)设置TabChanged事件的响应处理 public void setup ()这个函数后面介绍 TabHost.TabSpec 从上面的函数可以知道如何添加tab了要注意这里的Tag(标签)不是Tab按钮上的文字。 而要设置tab的label和content需要设置TabHost.TabSpec类。引用SDK里面的话——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”TabHost.TabSpec就是管理这3个东西 public String getTag () public TabHost.TabSpec setContent public TabHost.TabSpec setIndicator 我理解这里的 Indicator就是Tab上的label它可以 设置label setIndicator (CharSequence label) 或者同时 设置label和icon setIndicator (CharSequence label, Drawable icon) 或者直接 指定某个view setIndicator (View view) 对于 Content就是Tab里面的内容可以 设置View的id setContent(int viewId) 或者 TabHost.TabContentFactory的createTabContent(String tag)来处理 setContent(TabHost.TabContentFactory contentFactory) 或者用 new Intent来引入其他Activity的内容 setContent(Intent intent) 现在来看官方的Views/Tabs/Content By Id例子 代码 publicclassTabs1extendsTabActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); TabHosttabHost=getTabHost(); LayoutInflater.from(this).inflate(R.layout.tabs1,tabHost.getTabContentView(),true); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1") .setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab2") .setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(R.id.view3)); } } 原来在获取TabHost后需要用LayoutInflater来得到LayoutLayoutInflater在后面就详细介绍。R.layout.tabs1的内容 <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextViewandroid:id="@+id/view1" android:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/tabs_1_tab_1"/> <TextViewandroid:id="@+id/view2" android:background="@drawable/red" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/tabs_1_tab_2"/> <TextViewandroid:id="@+id/view3" android:background="@drawable/green" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/tabs_1_tab_3"/> </FrameLayout> <!--strings.xml <stringname="tabs_1_tab_1">tab1</string> <stringname="tabs_1_tab_2">tab2</string> <stringname="tabs_1_tab_3">tab3</string> --> 原来是用FrameLayout的 而让Tab1的内容显示tab1且背景为Blue是setContent(R.id.view1)这里引用了TextView1。现在就基本明白如何添加tab以及如何设置label和content了。 接下来看看Views/Tabs/Content By Factory的例子 代码 publicclassTabs2extendsTabActivityimplementsTabHost.TabContentFactory{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); finalTabHosttabHost=getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1",getResources().getDrawable(R.drawable.star_big_on)) .setContent(this)); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("tab2") .setContent(this)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(this)); } publicViewcreateTabContent(Stringtag){ finalTextViewtv=newTextView(this); tv.setText("Contentfortabwithtag"+tag); returntv; } } 可以看到通过override重写重新实现父类TabHost.TabContentFactory中的方法View createTabContent(String tag)来实现不同tab的不同content。同时在setContent的参数设置为相应的TabContentFactory。 原来createTabContent是在每个tab第一次显示时才调用的随后再次显示该tab就不会再次调用的我自己用Logcat查看到的这一点很关键就是说在createTabContent是在tab没有完全创建前调用的这意味在createTabContent里面是不能调用getCurrentTabView等之类的函数的否则就出错 至于Views/Tabs/Content By Intent例子就只是贴出代码不给截图了 publicclassTabs3extendsTabActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); finalTabHosttabHost=getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("list") .setContent(newIntent(this,List1.class))); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("photolist") .setContent(newIntent(this,List8.class))); //Thistabsetstheintentflagsothatitisrecreatedeachtime //thetabisclicked. tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("destroy") .setContent(newIntent(this,Controls2.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); } } 效果Tab1的内容是List1的ActivityTab2的是List8的ActivityTab3的是controls2.Activity。 TabHost.OnTabChangeListener TabHost.OnTabChangeListener接口只有一个抽象方法onTabChanged(String tagString)明显地在 onTabChanged(String tagString)方法里面swtich..case..来判断tagString分别处理就行了。 TabHost.setup() 在此贴出SDK doc里面的相关解释 public void setup () Since: API Level 1 Call setup() before adding tabs if loading TabHost using findViewById(). However, You do not need to call setup() after getTabHost() in TabActivity. Example: mTabHost = (TabHost)findViewById(R.id.tabhost); mTabHost.setup(); mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1"); //我的理解是如果要用到findViewById来获取TabHost然后add tabs的话需要在addTab前call setup(); public void setup (LocalActivityManager activityGroup) Since: API Level 1 If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity. Parameters activityGroup Used to launch activities for tab content. 本文转自 Icansoft 51CTO博客原文链接http://blog.51cto.com/android/315208

优秀的个人博客,低调大师

Android:UI控件AutoCompleteTextView、MultiAutoCompleteTextView、include、mer...

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 public class MainActivity extends Activity { /* * 1.ems 输入长度 * 2.inputType 输入类型 * * 3.completeTextView设置: * android:completionThreshold="1" 设置从第几个字母开始查询记录 * * 4.<requestFocus />默认获得焦点 * * 5.复用控件的方法:可再设置属性 * <include layout="@layout/relayout" * android:layout_width="match_parent" * android:layout_height="match_parent" * android:layout_below="@+id/autoCompleteTextView1" /> * * 6.当根布局是framelayout时,可以使用merge复用系统的布局,合并重复的layout * <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> * </merge> */ @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>( this , android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView cTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); cTextView.setAdapter(adapter); MultiAutoCompleteTextView mulcTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1); mulcTextView.setAdapter(adapter); mulcTextView.setTokenizer( new MultiAutoCompleteTextView.CommaTokenizer()); //tokenizer关键分隔符 String str = "dafs,asdfa,dafdaf,dasfdsaff,trhfd,gfdhyj,hgjff" ; StringTokenizer tokenizer = new StringTokenizer(str, "," ); while (tokenizer.hasMoreElements()) { Object nextments = tokenizer.nextElement(); System.out.println(nextments); } } static final String[] COUNTRIES = new String[] { "Afghanistan" , "Albania" , "Algeria" , "American Samoa" , "Andorra" , "Angola" , "Anguilla" , "Antarctica" , "Antigua and Barbuda" , "Argentina" , "Armenia" , "Aruba" , "Australia" , "Austria" , "Azerbaijan" , "Bahrain" , "Bangladesh" , "Barbados" , "Belarus" , "Belgium" , "Belize" , "Benin" , "Bermuda" , "Bhutan" , "Bolivia" }; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true ; } } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1203963,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册