这是一个模仿的小例子,记录我在编写过程中所碰到的少量问题。
这个是最终效果
主要详情上面的搜索布局是怎样实现的,注意的是该实现功能是在RelativeLayout布局下。咱们先在布局中放一个EditText控件,给父容器设置gravity为center,让EditText控件居中,并且给EditText的hint设置一个值。
android:gravity="center"
设置EditText的背景为圆角
先现在drawable下新建一个shape节点的XML文件。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFFFFF" /> <corners android:radius="6dp" /> </shape>
shape属性的具体含义就不多说了,推荐一篇博客自己去看 http://blog.csdn.net/wswqiang/article/details/6616306
而后新建完了,是时候用了,EditText的background属性设置为它
android:background="@drawable/title_search_bg"
将一个图片设置在EditText中
代码很简单,由于是在帧布局下,所以可以利用特心。在该ImageView中增加如下代码
android:layout_alignLeft="@id/searchText" android:layout_alignTop="@id/searchText" android:layout_alignBottom="@id/searchText"
searchText是EditText的Id值,意思就是说让ImageView的左边和上边,下边跟EditText控件对齐,这样即可以将ImageView设置在EditText中了。
嗯,似乎应该让hint提醒的搜索往右边走点,只要要在原来写的shape中加一个属性就行了
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFFFFF" /> <corners android:radius="6dp" /> <!-- 增加该属性 --> <padding android:left="24dp" /></shape>
在给ImageView一个margin值让图片略微往右边走点。
android:layout_marginLeft="6dp"
EditText的背景太白了,不好看,而且搜索的图片看不清,尽管在xml中设置过background的,但是我们还可以在代码中设置。
EditText searchText = (EditText) view.findViewById(R.id.searchText); searchText.getBackground().setAlpha(100);
意思就是说设置EditText的背景为半透明的。
这个时候就差不多(≧▽≦)/啦啦啦,不过当有光标锁定的时候,就会自动弹出输出法,并且还会影响布局(⊙﹏⊙)b。
还有一个问题,输入法弹出不影响activity原有布局
对于这个问题,网络上给出了很多处理方案,可以修改布局之类的就不说了:
1.在AndroidManifest.xml 中对应的activity下增加如下代码:
<activity
android:windowSoftInputMode="adjustPan"
android:name=".MainActivity"
android:label="@string/app_name"
至于windowSoftInputMode有哪些属性自己去看看 http://blog.csdn.net/liluo1217/article/details/6184169
2.在当前Activity的Java文件中增加如下代码 ,其实也就是通过Java代码增加第一种情况
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
接着上篇继续写,直到上篇结束后效果是这样滴。
先处理光标的样式,一般情况下,光标默认是黑色的。哪有没有办法给光标设置颜色呢,或者者在给光标设置粗细?
方法一定是有滴,在EditText中有一个textCursorDrawable属性,只要要对它进行修改就行了。
设置光标的颜色和粗细
1.设置为@null,意思就是说让光标颜色和text color 的颜色一样。
android:textCursorDrawable="@null"
2.给一个资源文件,可以设置光标的颜色和粗细。
android:textCursorDrawable="@drawable/color_cursor"
color_cursor.xml 文件如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<size android:width="4dp" />
<solid android:color="#FF0033" />
</shape>
效果如下 :
有的时候有没有觉得hint提醒的字体比较大,颜色不好看?尽管设置EditText的text和textSize属性,hint属性也会变,但是都是同步,能不能两者有区分呢?一定是可以滴
设置hint的颜色和大小
先是设置大小,设置大小在对象中正好有该方法。
searchText.setHintTextColor(fontColor);
但是设置颜色就没有直观的了,通过查找其实setHint()即可以做到。
方法如下:
CharSequence hint = searchText.getHint();
SpannableString ss = new SpannableString(hint);
AbsoluteSizeSpan ass = new AbsoluteSizeSpan(fontSize, true);
searchText.setHintTextColor(fontColor);
ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
searchText.setHint(new SpannedString(ss));
这个时候效果就差不多(≧▽≦)/啦啦啦
原文:http://blog.csdn.net/qq_24697659/article/details/49800419
Copyright © 2013-2021 xieniao.com. 血鸟 版权所有 景德镇送码科技有限公司 赣ICP备19014045号-2
增值电信业务许可证:赣B2-20200002 赣公网安备 36020302000062号