【发布时间】:2017-01-31 22:31:06
【问题描述】:
我正在创建一个自定义视图以与 android.widget.Toast.setView() 一起使用,但无论我如何调整视图大小,所有内容似乎都使用“wrap_content”作为布局宽度设置进行布局。
例如,无论我将视图的根布局(在我的情况下为线性布局)宽度设置为“match_parent”还是指定静态大小(例如“500dp”),我在运行时都会得到相同的结果...... toast 只是包装了内容.
我的布局 XML 是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toast_container"
android:orientation="horizontal"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="@drawable/toast_bg">
<ImageView
android:id="@+id/toastIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="@drawable/toast_icon" />
<TextView
android:id="@+id/toastMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample toast message"/>
</LinearLayout>
我怀疑 Android(我在 API 19 Kit-Kat 平台上运行)将 toast 视图强制为“wrap_content”。是这样吗?
我确定有人会建议我查看 AOSP 中的源代码。如果我手头有代码,我会这样做。
【问题讨论】:
标签: android toast android-toast