[英]Make TextView with drawable expand inside CardView
I'm trying to make a TextView
that contains a text and a drawable expand/scale based on the size of the TextView
.我正在尝试制作一个
TextView
,其中包含文本和基于TextView
大小的可绘制扩展/缩放。 My current code:我当前的代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".activity.WelcomeActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_view"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"
android:text="Example"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardStart"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="20dp"
app:layout_constraintDimensionRatio="1"
android:layout_gravity="fill"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardUseCompatPadding="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="@+id/txtCardStart"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:orientation="vertical"
android:text="@string/card_start"
android:textAlignment="center"
app:drawableTopCompat="@drawable/ic_baseline_start_24" />
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
which looks good, but small:看起来不错,但很小:
and when I make the TextView
take more space it doesn't scale proportionally, but rather stays as big as drawable height and width is defined in ic_baseline_start_24
:当我让
TextView
占用更多空间时,它不会按比例缩放,而是保持与ic_baseline_start_24
中定义的可绘制高度和宽度一样大:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="50dp"
android:height="50dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M14.59,7.41L18.17,11H6v2h12.17l-3.59,3.59L16,18l6,-6l-6,-6L14.59,7.41zM2,6v12h2V6H2z" />
</vector>
Is this possible with TextEdit
or I'd need to separate an ImageView
and a TextView
?这可能与
TextEdit
或我需要分开ImageView
和TextView
? My goal is to see it like this我的目标是这样看
You can use MaterialButton instead of MaterialCardView like below:您可以使用 MaterialButton 而不是 MaterialCardView,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".activity.WelcomeActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Example"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/txtCardStart"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="fill"
android:backgroundTint="@color/gray_light"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical"
android:text="Start"
android:textAlignment="center"
android:textStyle="bold"
app:cornerRadius="8dp"
app:cardElevation="8dp"
android:textSize="24sp"
app:cardUseCompatPadding="true"
app:icon="@drawable/ic_baseline_start_24"
app:iconGravity="textTop"
app:iconSize="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
note that you can set textSize and iconSize as you wish.请注意,您可以根据需要设置 textSize 和 iconSize。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.