按钮不显示(左)图像
Button does not display (left) image
我正在开发 Android 应用程序。在片段中,我需要放置一些带有文本和图像的按钮。我使用 drawableStart 属性来放置一个可绘制对象。关于它不显示图像的任何想法?
另外一个信息:我的项目正在使用 JetPack 库(最新版本)。
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:clipChildren="false"
android:fillViewport="true"
app:behavior_overlapTop="128dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.activities.DocumentoDetailActivity"
tools:showIn="@layout/activity_documento_detail">
<androidx.cardview.widget.CardView
android:id="@+id/documento_card"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/text_margin"
android:layout_marginTop="32dp"
android:layout_marginRight="@dimen/text_margin"
android:layout_marginBottom="@dimen/text_margin"
android:transitionName="@{@string/event_card_transition + eventDetailViewModel.event.id}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/documento_image_thumbnail"
android:layout_width="58dp"
android:layout_height="0dp"
android:background="@android:color/darker_gray"
android:contentDescription="@string/app_img_content_description"
android:cropToPadding="true"
android:elevation="@dimen/app_cardview_default_elevation"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="W,4:3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_thumbnail_laboratorio_analisi" />
<!--app:loadUrl="@{eventDetailViewModel.event.thumbnail}"-->
<!--android:transitionName="@{@string/event_image_transition + eventDetailViewModel.event.id}"-->
<!--<androidx.cardview.widget.CardView-->
<!--android:id="@+id/documento_preferred"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginTop="8dp"-->
<!--android:layout_marginEnd="8dp"-->
<!--android:transitionGroup="true"-->
<!--android:visibility="visible"-->
<!--app:cardBackgroundColor="#FFFFFF"-->
<!--app:cardElevation="@dimen/app_design_appbar_elevation"-->
<!--app:layout_constraintEnd_toEndOf="@+id/documento_card"-->
<!--app:layout_constraintRight_toRightOf="@id/documento_card"-->
<!--app:layout_constraintTop_toTopOf="@+id/documento_card">-->
<!--<ImageView-->
<!--android:id="@+id/documento_star"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginStart="8dp"-->
<!--android:layout_marginTop="8dp"-->
<!--android:layout_marginEnd="8dp"-->
<!--android:layout_marginBottom="8dp"-->
<!--app:srcCompat="@drawable/ic_item_unstarred" />-->
<!--android:text="@{eventDetailViewModel.event.price}"-->
<!--</androidx.cardview.widget.CardView>-->
<TextView
android:id="@+id/documento_titolo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:ellipsize="end"
android:maxLines="3"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:transitionName="@{@string/event_name_transition + eventDetailViewModel.event.id}"
app:layout_constraintLeft_toRightOf="@id/documento_image_thumbnail"
app:layout_constraintStart_toEndOf="@+id/documento_image_thumbnail"
app:layout_constraintTop_toTopOf="@+id/documento_card"
tools:text="@sample/documenti.json/data/titolo" />
<!--android:text="@{eventDetailViewModel.event.name}"-->
<TextView
android:id="@+id/documento_info1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:maxLines="1"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@color/primary_dark"
app:layout_constraintEnd_toEndOf="@+id/documento_card"
app:layout_constraintStart_toEndOf="@+id/documento_info1_label"
app:layout_constraintTop_toBottomOf="@+id/documento_titolo"
tools:text="@sample/documenti.json/data/info" />
<!--android:text="@{eventDetailViewModel.venue.name}"-->
<TextView
android:id="@+id/documento_info1_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="Data:"
app:layout_constraintStart_toEndOf="@+id/documento_image_thumbnail"
app:layout_constraintTop_toBottomOf="@+id/documento_titolo" />
<Button
android:id="@+id/documento_btn_cancella"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="@string/documento_cancella"
android:drawableStart="@drawable/ic_action_delete_24dp"
app:layout_constraintStart_toStartOf="@+id/documento_card"
app:layout_constraintTop_toBottomOf="@+id/documento_info1" />
<Button
android:id="@+id/documento_btn_modifica"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="@string/documento_modifica"
android:drawableStart="@drawable/ic_action_delete_24dp"
app:layout_constraintEnd_toStartOf="@+id/documento_btn_vedi"
app:layout_constraintStart_toEndOf="@+id/documento_btn_cancella"
app:layout_constraintTop_toBottomOf="@+id/documento_info1" />
<Button
android:id="@+id/documento_btn_vedi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="@string/documento_vedi"
android:drawableStart="@drawable/ic_action_search__white_24dp"
app:layout_constraintEnd_toEndOf="@+id/documento_card"
app:layout_constraintTop_toBottomOf="@+id/documento_info1" />
<!--android:rating="@{eventRatingViewModel.result.rating}"-->
<androidx.viewpager.widget.ViewPager
android:id="@+id/documento_view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/text_margin"
android:layout_marginTop="8dp"
android:layout_marginRight="@dimen/text_margin"
android:layout_marginBottom="@dimen/text_margin"
android:elevation="4dp"
android:transitionGroup="true"
app:layout_constraintBottom_toBottomOf="@id/documento_card"
app:layout_constraintLeft_toLeftOf="@id/documento_card"
app:layout_constraintRight_toRightOf="@id/documento_card"
app:layout_constraintTop_toBottomOf="@+id/documento_btn_vedi">
<!--app:pageMargin="@{@dimen/activity_horizontal_margin}}"-->
<!--app:sectionAdapter="@{eventDetailViewModel.event.id}"-->
<!--app:pageMargin="@dimen/activity_horizontal_margin"-->
<com.google.android.material.tabs.TabLayout
android:id="@+id/documento_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/AppTabLayoutStyle"
/>
<!--app:setupWithViewPager="@{viewPager}"-->
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
<!--</layout>-->
提前致谢!
因为您将 layout_width
设置为 wrap_content
,可绘制对象缺少要绘制的宽度。
试试把它改成这样:
<android.support.v7.widget.AppCompatButton
android:id="@+id/documento_btn_modifica"
android:layout_width="0dp"
android:layout_height="56dp"
android:drawableStart="@drawable/ic_action_delete_24dp"
app:layout_constraintDimensionRatio="1:1"
/>
这样 app:layout_constraintDimensionRatio="1:1"
和 android:layout_width="0dp"
将确保宽度与高度匹配。
终于找到了。我在这里写信来分享我的解决方案。我解决了使用 app:icon
属性而不是 android:drawableStart
.
我正在开发 Android 应用程序。在片段中,我需要放置一些带有文本和图像的按钮。我使用 drawableStart 属性来放置一个可绘制对象。关于它不显示图像的任何想法?
另外一个信息:我的项目正在使用 JetPack 库(最新版本)。
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:clipChildren="false"
android:fillViewport="true"
app:behavior_overlapTop="128dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.activities.DocumentoDetailActivity"
tools:showIn="@layout/activity_documento_detail">
<androidx.cardview.widget.CardView
android:id="@+id/documento_card"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/text_margin"
android:layout_marginTop="32dp"
android:layout_marginRight="@dimen/text_margin"
android:layout_marginBottom="@dimen/text_margin"
android:transitionName="@{@string/event_card_transition + eventDetailViewModel.event.id}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/documento_image_thumbnail"
android:layout_width="58dp"
android:layout_height="0dp"
android:background="@android:color/darker_gray"
android:contentDescription="@string/app_img_content_description"
android:cropToPadding="true"
android:elevation="@dimen/app_cardview_default_elevation"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="W,4:3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_thumbnail_laboratorio_analisi" />
<!--app:loadUrl="@{eventDetailViewModel.event.thumbnail}"-->
<!--android:transitionName="@{@string/event_image_transition + eventDetailViewModel.event.id}"-->
<!--<androidx.cardview.widget.CardView-->
<!--android:id="@+id/documento_preferred"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginTop="8dp"-->
<!--android:layout_marginEnd="8dp"-->
<!--android:transitionGroup="true"-->
<!--android:visibility="visible"-->
<!--app:cardBackgroundColor="#FFFFFF"-->
<!--app:cardElevation="@dimen/app_design_appbar_elevation"-->
<!--app:layout_constraintEnd_toEndOf="@+id/documento_card"-->
<!--app:layout_constraintRight_toRightOf="@id/documento_card"-->
<!--app:layout_constraintTop_toTopOf="@+id/documento_card">-->
<!--<ImageView-->
<!--android:id="@+id/documento_star"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginStart="8dp"-->
<!--android:layout_marginTop="8dp"-->
<!--android:layout_marginEnd="8dp"-->
<!--android:layout_marginBottom="8dp"-->
<!--app:srcCompat="@drawable/ic_item_unstarred" />-->
<!--android:text="@{eventDetailViewModel.event.price}"-->
<!--</androidx.cardview.widget.CardView>-->
<TextView
android:id="@+id/documento_titolo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:ellipsize="end"
android:maxLines="3"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:transitionName="@{@string/event_name_transition + eventDetailViewModel.event.id}"
app:layout_constraintLeft_toRightOf="@id/documento_image_thumbnail"
app:layout_constraintStart_toEndOf="@+id/documento_image_thumbnail"
app:layout_constraintTop_toTopOf="@+id/documento_card"
tools:text="@sample/documenti.json/data/titolo" />
<!--android:text="@{eventDetailViewModel.event.name}"-->
<TextView
android:id="@+id/documento_info1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:maxLines="1"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@color/primary_dark"
app:layout_constraintEnd_toEndOf="@+id/documento_card"
app:layout_constraintStart_toEndOf="@+id/documento_info1_label"
app:layout_constraintTop_toBottomOf="@+id/documento_titolo"
tools:text="@sample/documenti.json/data/info" />
<!--android:text="@{eventDetailViewModel.venue.name}"-->
<TextView
android:id="@+id/documento_info1_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="Data:"
app:layout_constraintStart_toEndOf="@+id/documento_image_thumbnail"
app:layout_constraintTop_toBottomOf="@+id/documento_titolo" />
<Button
android:id="@+id/documento_btn_cancella"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="@string/documento_cancella"
android:drawableStart="@drawable/ic_action_delete_24dp"
app:layout_constraintStart_toStartOf="@+id/documento_card"
app:layout_constraintTop_toBottomOf="@+id/documento_info1" />
<Button
android:id="@+id/documento_btn_modifica"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="@string/documento_modifica"
android:drawableStart="@drawable/ic_action_delete_24dp"
app:layout_constraintEnd_toStartOf="@+id/documento_btn_vedi"
app:layout_constraintStart_toEndOf="@+id/documento_btn_cancella"
app:layout_constraintTop_toBottomOf="@+id/documento_info1" />
<Button
android:id="@+id/documento_btn_vedi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_marginEnd="8dp"
android:elevation="@dimen/app_cardview_default_elevation"
android:text="@string/documento_vedi"
android:drawableStart="@drawable/ic_action_search__white_24dp"
app:layout_constraintEnd_toEndOf="@+id/documento_card"
app:layout_constraintTop_toBottomOf="@+id/documento_info1" />
<!--android:rating="@{eventRatingViewModel.result.rating}"-->
<androidx.viewpager.widget.ViewPager
android:id="@+id/documento_view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/text_margin"
android:layout_marginTop="8dp"
android:layout_marginRight="@dimen/text_margin"
android:layout_marginBottom="@dimen/text_margin"
android:elevation="4dp"
android:transitionGroup="true"
app:layout_constraintBottom_toBottomOf="@id/documento_card"
app:layout_constraintLeft_toLeftOf="@id/documento_card"
app:layout_constraintRight_toRightOf="@id/documento_card"
app:layout_constraintTop_toBottomOf="@+id/documento_btn_vedi">
<!--app:pageMargin="@{@dimen/activity_horizontal_margin}}"-->
<!--app:sectionAdapter="@{eventDetailViewModel.event.id}"-->
<!--app:pageMargin="@dimen/activity_horizontal_margin"-->
<com.google.android.material.tabs.TabLayout
android:id="@+id/documento_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/AppTabLayoutStyle"
/>
<!--app:setupWithViewPager="@{viewPager}"-->
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
<!--</layout>-->
提前致谢!
因为您将 layout_width
设置为 wrap_content
,可绘制对象缺少要绘制的宽度。
试试把它改成这样:
<android.support.v7.widget.AppCompatButton
android:id="@+id/documento_btn_modifica"
android:layout_width="0dp"
android:layout_height="56dp"
android:drawableStart="@drawable/ic_action_delete_24dp"
app:layout_constraintDimensionRatio="1:1"
/>
这样 app:layout_constraintDimensionRatio="1:1"
和 android:layout_width="0dp"
将确保宽度与高度匹配。
终于找到了。我在这里写信来分享我的解决方案。我解决了使用 app:icon
属性而不是 android:drawableStart
.