[英]How to make button with text+icon the same size as button with text only
[英]How to make icon the same size as text in Android?
我有一些文字的 TextView 和一些图标的 ImageView。 我按基线对齐它们:
android:baselineAlignBottom="true"
app:layout_constraintBaseline_toBaselineOf="@+id/penny"
TextView 有一些文字大小,但我无法猜测 ImageView 的高度,因此文字(不是 TextView)和图标的高度相同,它们总是略有不同。 我该如何解决?
<androidx.constraintlayout.widget.ConstraintLayout ... >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:baselineAlignBottom="true"
app:layout_constraintBaseline_toBaselineOf="@+id/text"
app:layout_constraintStart_toEndOf="@+id/text"
android:src="@drawable/icon" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以将图像视图的顶部和底部分别设置为 textView 的顶部和底部,高度 imageView 为0dp
,scaleType 为 fitXY。
你可以试试这个,它可能对你有用。
使线性布局horizontal
, height
为wrap_content
,取 Textview 与您需要的文字大小。 以height
为match_parent
和width
为wrap_content
的图像视图。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demo Text"
android:textColor="@color/black"
android:textSize="@dimen/_30sdp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_app_logo" />
</LinearLayout>
对于 ConstraintLayout 布局,您可以尝试如下操作。
设置android:scaleType="fitXY"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvDemoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demo Text"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textSize="@dimen/_35sdp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintTop_toTopOf="@id/tvDemoText"
app:layout_constraintBottom_toBottomOf="@id/tvDemoText"
app:layout_constraintStart_toEndOf="@id/tvDemoText"
android:src="@drawable/material_drawer_ico_menu_down" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用sp
单位作为图像尺寸,因此图像将始终根据用户系统字体大小设置进行缩放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.