EditText之padding属性

来源:互联网 发布:软件行业会计核算 编辑:程序博客网 时间:2024/06/05 06:41
看到好多同学都在问EditText中的前几个字不可以被编辑 ,如图:



这其实是个TextView和EditText的结合。

我们看下代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.edittextdemo.MainActivity$PlaceholderFragment" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <EditText        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="62dp"        android:background="@drawable/test"        android:ems="10"        android:paddingLeft="50dip" >        <requestFocus />    </EditText>    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/editText1"        android:layout_alignBottom="@+id/editText1"        android:layout_alignLeft="@+id/editText1"        android:layout_marginLeft="14dp"        android:text="账号" /></RelativeLayout>

其中最重要的就是这个paddingleft属性,他可以让输入的内容距离左侧多少像素,然后将准备的textView放到前面就可以了  
layout_alignBaseline
这个基准线属性,是为了“账号”这两个字和你要输入的文字是在一个水平线上书写的,不会乱

android:background="@drawable/test"
这个EditText的background属性里面我做了个特效,让输入点击的时候边框变化
继续上代码:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_enabled="false" android:drawable="@drawable/login_input"></item>    <item android:state_pressed="true" android:drawable="@drawable/login_input"></item>    <item android:state_focused="true" android:drawable="@drawable/input_over"></item></selector>

我使用android背景选择器selector 自定义的一个xml文件
这是其中的属性及解释
android:drawable 放一个drawable资源android:state_pressed 是否按下,如一个按钮触摸或者点击。android:state_focused 是否取得焦点,比如用户选择了一个文本框。android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。android:state_checked 被checked了,如:一个RadioButton可以被check了。android:state_enabled 能够接受触摸或者点击事件android:state_activated 被激活android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了



0 0