net*_*men 6 layout android margins padding android-edittext
我有这样的EditText定义:
<EditText
android:id="@+id/input_password"
android:hint="@string/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword" />
Run Code Online (Sandbox Code Playgroud)
它有一些默认的填充,看起来像"Show layout bounds"选项:
(或者它真的是边缘,我不确定.我指的是红色内盒和蓝色角落之间的空间).但这些填充/边距的价值是多少?在platforms/android-19/data/res/values/themes.xml我发现的Widget.EditText样式定义如下:
<style name="Widget.EditText">
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:clickable">true</item>
<item name="android:background">?android:attr/editTextBackground</item>
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
<item name="android:textColor">?android:attr/editTextColor</item>
<item name="android:gravity">center_vertical</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是没有提到任何边距/填充.那他们在哪里定义?
小智 2
这个问题来自于默认值?android:attr/editTextBackground。
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="@dimen/abc_edit_text_inset_bottom_material"
android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
android:insetTop="@dimen/abc_edit_text_inset_top_material">
<selector>
<item
android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
android:state_enabled="false" />
<item
android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
android:state_focused="false"
android:state_pressed="false" />
<item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha" />
</selector>
</inset>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,EditText 所有边缘的插入值为 4dp。这使得 EditText 的内容周围有很小的填充。要删除该填充,您应该创建一个具有修改属性的新 EditText 样式editTextBackground。例如:
<style name="Widget.App.TextField" parent="@style/Widget.AppCompat.EditText">
<item name="colorControlActivated">#303E44</item>
<item name="colorControlHighlight">#E5E9EC</item>
<item name="colorControlNormal">#E5E9EC</item>
<item name="editTextBackground">@drawable/background_new_edit_text</item>
<item name="android:editTextBackground">@drawable/background_new_edit_text</item>
</style>
Run Code Online (Sandbox Code Playgroud)
新的background_new_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:inset="@dimen/spacing_0dp">
<selector>
<item
android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
android:state_enabled="false" />
<item
android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
android:state_focused="false"
android:state_pressed="false" />
<item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha" />
</selector>
</inset>
Run Code Online (Sandbox Code Playgroud)
将新风格应用到您的EditText:
<EditText
android:id="@+id/edt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Widget.App.TextField"
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4154 次 |
| 最近记录: |