【发布时间】:2021-04-02 02:32:08
【问题描述】:
我必须设计这个布局。图标应该坚持textview。
这是我尝试过的,看起来正是需要的。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/padding_m"
android:paddingTop="@dimen/padding_s"
android:paddingEnd="@dimen/padding_m"
android:paddingBottom="@dimen/padding_s"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/title_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/more_options"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/active_group_name"
style="@style/AppbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
app:layout_constrainedWidth="true"
tools:text="Ranjan Malav" />
<FrameLayout
android:id="@+id/account_option_icon_container"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_marginStart="@dimen/margin_xs"
android:background="@drawable/circle_background_36dp">
<ImageView
android:id="@+id/account_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_outline_settings_24" />
</FrameLayout>
</LinearLayout>
<ImageView
android:id="@+id/more_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_more_vert_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我怎样才能给图标优先级,让它占据空间,然后 textview 应该将自己包裹在剩余的空间中?
【问题讨论】:
-
我会说这很容易做到,但为什么不使用
TextView的drawableEnd属性中的图标,这样就无需使用单独的小部件ImageView? -
@Lalit 这需要创建一个带有圆形背景的新drawable。我想保持动态,这样我就可以以编程方式更改背景色调。
-
您已经在做的是使用额外的两个小部件(FrameLayout 和 ImageView)和两个可绘制对象(背景和设置图标),它们可以替换为直接应用于 TextView 的图层列表可绘制对象。 TextView 的可绘制对象的背景色调可以动态更改,我相信使用 PorterDuffXMode,您还可以将色调应用于仅可绘制到背景的图层列表。
-
@LalitFauzdar 感谢 Lalit 的分享,我稍后会尝试实现。
标签: android android-layout android-constraintlayout