TextView drawableLeft图标与首行文字对齐问题

drawableLeft 的图标总是和第一行文字对不齐?嗯,这问题我也踩过坑。TextView是 Android 里最常用的控件之一,左边加个小图标挺常见,用drawableLeft就能搞定。但一旦文字多了点,图标就跑中间去了,看着就别扭。

图标默认会垂直居中,这就导致跟第一行文字对不齐。想其实不难,方法还挺多的。比如,试试调一下android:drawablePadding,把图标往上推一推。有时候调几次间距就能看顺眼了。

要是还不满意,可以动动gravitytextGravity。前者管图文整体位置,后者只管字。你设成top看看,图标跟文字就都贴上去了。不过这俩属性有时候不太听话,看情况用。

再不行就自定义一个TextView吧,自己重写onDraw(),图标位置你说了算。在我写的TextViewDemo小项目里,就专门演示了这个过程,整个流程清晰又实用。

建议你平时多观察 UI 的细节,有时候一像素的偏差,就能让整个界面看着舒服不少。如果你碰上图文对齐的问题,不妨参考下这个例子,调起来不费劲,效果还不错。

zip
TextViewDemo.zip 预估大小:75个文件
folder
TextViewDemo 文件夹
file
gradlew.bat 2KB
file
gradlew 5KB
file
gradle.properties 747B
folder
.gradle 文件夹
folder
3.3 文件夹
folder
tasks 文件夹
folder
_app_compileDebugJavaWithJavac 文件夹
folder
localJarClasspathSnapshot 文件夹
file
localJarClasspathSnapshot.bin 20KB
file
localJarClasspathSnapshot.lock 17B
folder
localClassSetAnalysis 文件夹
file
localClassSetAnalysis.lock 17B
file
localClassSetAnalysis.bin 22KB
folder
taskArtifacts 文件夹
file
fileHashes.bin 133KB
file
taskArtifacts.bin 59KB
file
fileSnapshots.bin 988KB
file
taskArtifacts.lock 17B
folder
app 文件夹
file
proguard-rules.pro 941B
folder
src 文件夹
folder
test 文件夹
folder
java 文件夹
folder
itcast 文件夹
folder
text 文件夹
folder
com 文件夹
folder
textviewdemo 文件夹
file
ExampleUnitTest.java 422B
folder
androidTest 文件夹
folder
java 文件夹
folder
itcast 文件夹
folder
text 文件夹
folder
com 文件夹
folder
textviewdemo 文件夹
file
ExampleInstrumentedTest.java 786B
folder
main 文件夹
folder
res 文件夹
folder
mipmap-xxxhdpi 文件夹
file
ic_launcher.png 10KB
file
ic_launcher_round.png 14KB
folder
drawable 文件夹
folder
mipmap-hdpi 文件夹
file
ic_launcher.png 3KB
file
ic_launcher_round.png 4KB
folder
values 文件夹
file
colors.xml 214B
file
strings.xml 78B
file
attrs.xml 680B
file
styles.xml 394B
folder
mipmap-xxhdpi 文件夹
file
ic_launcher.png 8KB
file
ic_task_gold_phone.png 345B
file
ic_launcher_round.png 10KB
folder
mipmap-mdpi 文件夹
file
ic_launcher.png 2KB
file
ic_launcher_round.png 2KB
folder
mipmap-xhdpi 文件夹
file
ic_launcher.png 5KB
file
ic_launcher_round.png 6KB
folder
layout 文件夹
file
activity_main.xml 1KB
folder
java 文件夹
folder
itcast 文件夹
folder
text 文件夹
folder
com 文件夹
folder
textviewdemo 文件夹
file
MainActivity.java 354B
file
TextViewDrawable.java 4KB
file
AndroidManifest.xml 746B
file
app.iml 10KB
file
.gitignore 8B
file
build.gradle 973B
folder
libs 文件夹
folder
gradle 文件夹
folder
wrapper 文件夹
file
gradle-wrapper.jar 52KB
file
gradle-wrapper.properties 236B
folder
.idea 文件夹
file
gradle.xml 626B
file
runConfigurations.xml 564B
file
misc.xml 4KB
folder
copyright 文件夹
file
profiles_settings.xml 76B
file
modules.xml 361B
file
compiler.xml 686B
file
workspace.xml 158KB
folder
libraries 文件夹
file
javawriter_2_1_1.xml 330B
file
support_core_ui_26_0_0_alpha1.xml 595B
file
jsr305_2_0_1.xml 330B
file
support_annotations_26_0_0_alpha1.xml 530B
file
rules_0_5.xml 540B
file
javax_annotation_api_1_2.xml 358B
file
support_v4_26_0_0_alpha1.xml 580B
file
support_vector_drawable_26_0_0_alpha1.xml 619B
file
support_compat_26_0_0_alpha1.xml 592B
file
runner_0_5.xml 543B
file
hamcrest_library_1_3.xml 342B
file
espresso_idling_resource_2_2_2.xml 612B
file
appcompat_v7_26_0_0_alpha1.xml 586B
file
animated_vector_drawable_26_0_0_alpha1.xml 622B
file
constraint_layout_solver_1_0_2.xml 337B
file
support_core_utils_26_0_0_alpha1.xml 604B
file
hamcrest_core_1_3.xml 526B
file
junit_4_12.xml 477B
file
constraint_layout_1_0_2.xml 410B
file
support_fragment_26_0_0_alpha1.xml 598B
file
hamcrest_integration_1_3.xml 354B
file
espresso_core_2_2_2.xml 579B
file
javax_inject_1.xml 511B
file
support_media_compat_26_0_0_alpha1.xml 610B
file
exposed_instrumentation_api_publish_0_5.xml 630B
file
.gitignore 127B
file
local.properties 496B
file
settings.gradle 16B
file
TextViewDemo.iml 866B
file
build.gradle 521B
...
zip 文件大小:385.37KB