Android TextView前加图标垂直居中第一行(仿大众点评购买须知/提示语)
16lz
2021-01-23
如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用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>
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)
更多相关文章
- 5、控件系列之TOAST(吐司提示)的曾经、现在与未来
- Android RecyclerView 实现瀑布流交错效果,并使最后一行子View高
- android 获取屏幕高度,宽度,状态栏高度
- 谁说Android不行了,以个人能力的不行来否定整个市场的需求是弱者
- Android 动态设置控件高度
- android scrollview嵌套listview出现高度显示不全解决方案
- Android 高亮指示层提示
- Android 升级Android Studio升级3.5提示下载fastutil插件