Android学习笔记
来源:互联网 发布:淘宝模板上传宝贝图 编辑:程序博客网 时间:2024/05/18 01:57
前言
以下收藏一些Android学习过程中比较有用东西
文章链接
ANDROID SUPPORT兼容包详解
android基础–tools:context=”.MainActivity”作用
1.线性布局设置分割线:
首先给出分割线的样式,也可以使一个图片文件,这里我们用shape
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:height="2px"/> <solid android:color="@android:color/holo_orange_dark"/></shape>
然后在线性布局中设置
android:divider="@drawable/divider_line" //显示位置 android:showDividers="middle" //距离两边的间距 android:dividerPadding="10px"
2.GridLayout的使用
<GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" //设置几行 android:rowCount="2" //设置几列 android:columnCount="4" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好" //跨几行 android:layout_rowSpan="2" //必须设置 android:layout_gravity="fill" /> <Button android:text="Button" /> <Button android:text="你好" /> <Button android:text="Button" /> <Button android:text="你好" /> <Button android:text="Button" /> </GridLayout>
3.带阴影的文字
android:shadowDx="10" android:shadowDy="10" android:shadowColor="#00ff00" android:shadowRadius="3"
4.带边框的文字
<TextView android:layout_width="200dp" android:layout_height="50dp" android:text="我是文字" android:gravity="center" android:textSize="20sp" android:background="@drawable/divider_line" android:textColor="@android:color/white" /> <TextView android:layout_width="200dp" android:layout_height="50dp" android:layout_marginTop="10dp" android:text="我是文字" android:gravity="center" android:textSize="20sp" android:background="@drawable/divider_line2" android:textColor="@android:color/holo_blue_bright" />
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:color="@android:color/black" android:width="3px" /> <corners android:radius="5dp"/> <solid android:color="@android:color/holo_orange_dark"/></shape>
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:color="@android:color/black" android:width="3px" /> <corners android:radius="5dp"/> <gradient android:angle="90" android:startColor="@android:color/white" android:endColor="@android:color/transparent" /></shape>
gradient设置渐变,angle设置从哪里开始渐变,0代表左边,然后逆时针,依次是90,180,270,下,右,上
5.设置文字周围图片大小
//获取图片资源数组 Drawable[] drawables = tv.getCompoundDrawables(); //设置上方图片大小,方向为左上右下,索引依次从0-3 drawables[1].setBounds(0,0,200,200); tv.setCompoundDrawables(drawables[0],drawables[1],drawables[2],drawables[3]);
6.文字设置autoLink
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电话号码:18201431960" android:autoLink="phone" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="邮箱:1615404043@qq.com" android:autoLink="email" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="百度:http://www.baidu.com" android:autoLink="web" android:textSize="20sp" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Java代码设置
tv.setText("中国电信:13399898832");// 设置autoLink类型 tv.setAutoLinkMask(Linkify.PHONE_NUMBERS);// 设置textview的行动方式 tv.setMovementMethod(LinkMovementMethod.getInstance());
设置文字滚动
<TextView android:id="@+id/tv" android:layout_width="200dp" android:layout_height="30dp" android:text="大幅扩大数据库法律进行产能,阿斯蒂芬就是快点放假打三分斯蒂芬斯蒂芬地方" android:textSize="20sp" android:scrollbars="none" />
tv = (TextView) findViewById(R.id.tv); tv.setMovementMethod(ScrollingMovementMethod.getInstance());
7.TextView与html结合使用
常用的标签:
<font>:设置颜色和字体。<big>:设置字体大号<small>:设置字体小号<i><b>:斜体粗体<a>:连接网址<img>:图片
String str = "<font color='blue'>百度一下,你就知道<font><br/>"; str += "<a href='http://www.baidu.com'>百度一下<i>adsf</i></a>"; tv.setText(Html.fromHtml(str)); tv.setMovementMethod(LinkMovementMethod.getInstance());
插入图片
tv = (TextView) findViewById(R.id.tv); String str = "图片:<img src='ic_launcher'/>"; tv.setText(Html.fromHtml(str, new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = null; try { Field field = R.drawable.class.getField(source);// 返回对象中字段的值 int resourceId = Integer.parseInt(field.get(null).toString()); drawable = getResources().getDrawable(resourceId); drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); } catch (Exception e) { e.printStackTrace(); } return drawable; } },null));
tv = (TextView) findViewById(R.id.tv); tv2 = (TextView) findViewById(R.id.tv2); SpannableString span = new SpannableString("红色打电话斜体删除线绿色下划线图片:");// 文本颜色 span.setSpan(new ForegroundColorSpan(Color.RED),0,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 文本超链接 span.setSpan(new URLSpan("tel:18201431960"),2,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 字体样式 span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),5,7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //删除线 span.setSpan(new StrikethroughSpan(),7,10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new ForegroundColorSpan(Color.GREEN),10,12,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 下划线 span.setSpan(new UnderlineSpan(),12,15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置图片 Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher); drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BOTTOM); span.setSpan(imageSpan,17,18,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(span); //========================================================其他span String str = "公共条款"; SpannableString spannableString = new SpannableString(str); spannableString.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(MainActivity.this,"点击了",Toast.LENGTH_SHORT).show(); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(Color.BLUE); ds.setUnderlineText(false); } },0,str.length(),0);// 必须设置,否则没点击效果 tv2.setMovementMethod(LinkMovementMethod.getInstance()); tv2.setText(spannableString);// 去掉文字点击后北京高亮 tv2.setHighlightColor(Color.TRANSPARENT);
效果图
tv = (TextView) findViewById(R.id.tv); tv2 = (TextView) findViewById(R.id.tv2); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.append("好友"+i+ ", "); } String likeUsers = sb.substring(0,sb.length()-1); tv.setMovementMethod(LinkMovementMethod.getInstance()); //添加图片 Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher); drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); ImageSpan imagespan = new ImageSpan(drawable); SpannableString span = new SpannableString(" "); span.setSpan(imagespan,0,1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); SpannableStringBuilder ssb = new SpannableStringBuilder(span); //给图片后面添加文字 ssb.append(likeUsers); String[] result = likeUsers.split(","); if (result.length > 0) { for (int i = 0; i < result.length; i++) { final String name = result[i]; int start = likeUsers.indexOf(name)+span.length(); ssb.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show(); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(Color.BLUE); ds.setUnderlineText(false); } },start,start + name.length(),0); } tv.setText(ssb.append("等"+result.length+"人觉得很赞")); tv.setHighlightColor(Color.TRANSPARENT); }
设置TextView水平缩放和文字之间的行间距
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="我不知道" android:textScaleX="2" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="我不知道\n你好吗" android:lineSpacingExtra="10dp" />
设置上下标
设置上标 SuperscriptSpan superscriptSpan = new SuperscriptSpan(); SpannableString spannableString = new SpannableString("X\""); spannableString.setSpan(superscriptSpan,1,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(spannableString);// 设置下标 SubscriptSpan subscriptSpan = new SubscriptSpan(); SpannableString spannableString2 = new SpannableString("X1"); spannableString2.setSpan(subscriptSpan,1,2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv2.setText(spannableString2);
EditText
//获得焦点后选中所有内容android:selectAllOnFocus="true"
进入页面自动弹出软键盘,只需要两步
1.让edittext获取焦点
2.设置所在Activity
android:windowSoftInputMode="stateVisible"
setCompoundDrawablesWithIntrinsicBounds
在控件周围设置图片
public class EditTextWithDel extends android.support.v7.widget.AppCompatEditText { private static final String TAG = "EditTextWithDel"; private Drawable drawable; public EditTextWithDel(Context context) { this(context,null); } public EditTextWithDel(Context context, AttributeSet attrs) { super(context, attrs); init(); } public void init(){ drawable = getResources().getDrawable(R.drawable.icon_delete); addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { setDrawable(); } }); setDrawable(); } public void setDrawable(){ if(length() < 1) { setCompoundDrawablesWithIntrinsicBounds(null,null,null,null); } else { setCompoundDrawablesWithIntrinsicBounds(null,null, drawable,null); } } @Override public boolean onTouchEvent(MotionEvent event) { if(drawable!=null&&event.getAction() == MotionEvent.ACTION_UP) { int eventX = (int) event.getRawX(); int eventY = (int) event.getRawY(); Log.d(TAG, "onTouchEvent: eventX " + eventX+"eventY"+eventY); Rect rect = new Rect();// 获取视图在屏幕中的可见坐标 getGlobalVisibleRect(rect);// 设定矩形区域 rect.left = rect.right - 50; if (rect.contains(eventX,eventY)) { setText(""); } } return super.onTouchEvent(event); }}
下面以我的魅蓝note3为例,来获取各个部分的尺寸,这里不考虑ActionBar,我们在主题中不使用ActionBar
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); int widthPixels = displayMetrics.widthPixels; int heightPixels = displayMetrics.heightPixels; Log.d(TAG, "屏幕宽高: " +widthPixels+":"+heightPixels); Rect rect = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); Log.d(TAG, "引用程序APP区域: "+rect.width()+":"+rect.height()); Rect rect2 = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(rect2); int statusBarHeight = rect2.top; Log.d(TAG, "状态栏高度: "+statusBarHeight); Rect rect3 = new Rect(); getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect3); Log.d(TAG, "View布局区域: "+rect3.width()+":"+rect3.height()); }
结果如下:
0 0
- Android学习笔记--Gson笔记
- Android学习笔记
- Android学习笔记(二)
- android学习笔记--序
- android学习笔记1
- android学习笔记2
- android学习零碎笔记
- Android 学习笔记
- Android学习笔记(一)
- Android ExpandableListActivity 学习笔记
- Android EditText 学习笔记
- Android学习笔记3
- Android学习笔记4
- Android学习笔记5
- Android学习笔记9
- Android Animation学习笔记
- Android 学习笔记
- Android 数字签名学习笔记
- Java版数独算法实现
- 六、逻辑回归与最大熵模型
- 一些Java经典算法
- Java权重分配的实现算法
- 面向对象的RMS操作
- Android学习笔记
- PHP 点击链接更新数据库,返回值
- 使用jsr172解析xml
- j2me读取中文的配置文件
- 当显示内容过多时的滚屏类Container.java
- 对java字符串的各种操作方法和应用
- j2me代理连接返回码总是400的问题
- 按键检测
- Android SDK 2.2 开发环境安装