如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用android:drawableLeft 来做,可做完发现:当TextView内容为单行的时候是没有问题的,多行的时候,添加的这个drawableLeft就随着TextView高度而垂直居中了,变成下面的样子

看了下发现android:drawableLeft &android:drawableRight 是始终垂直居中于高度的

翻来翻去没有找到解决方法,就决定用简单粗暴的方法:

布局:


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:id="@+id/act_item_experience_datail_tip_layout"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:background="@color/black"              android:orientation="horizontal">    <ImageView        android:id="@+id/tip_img"        android:layout_width="wrap_content"        android:src="@mipmap/tip_left"        android:layout_height="match_parent"/>    <TextView        android:id="@+id/tip_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textColor="@color/gold"/></LinearLayout>


margin、lineSpacingExtra这些随喜好设置



LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);            lp.setMargins(0, (int)(mTipText.getLineHeight()*0.5), 0, 0);            mTipImageView.setLayoutParams(lp);

通过mTipText.getLineHeight() 来获得TextView的行高,再设置行高的一半Margin就能得到下面的效果

(注:如果设置了Padding或者Margin,mTipText.getLineHeight()*0.5 这就不一定是行高的一半(0.5)了,慢慢调整,设置过Padding和Margin后,会大于0.5)



更多相关文章

  1. Android(安卓)Studio常用设置
  2. android点滴(29) android中设置用户自定义的字体
  3. Android(安卓)Studio 小技巧(2):AS中Button文字默认大写的问题
  4. android中的Notification使用
  5. Android(安卓)Schema的妙用
  6. Android(安卓)Canvas类介绍
  7. Android(安卓)Studio App设置线性布局LinerLayout控件垂直/水平
  8. Android闹钟设置的解决方案
  9. TextView属性大全+单行显示长文本

随机推荐

  1. Android(安卓)有些机型hint不显示
  2. Android(安卓)ConstraintLayout 约束布局
  3. Android(安卓)基础常见记录
  4. android中在代码中创建应用的快捷图标
  5. Android(安卓)实现永久保存数据的方法详
  6. android启动之子系统切换
  7. Android(安卓)ViewFlipper实现多个布局手
  8. Android(安卓)Studio gradle sync failed
  9. android 编译碰到的问题及解决办法:(ubun
  10. (android)向sdcard中添加文件出现Failed