我已经为我的 Toast 消息创建了自定义 View 。
我希望我的 Toast 消息占据屏幕的宽度,左右边距为 30dp。
但如果 Toast 消息的文本/内容很长,它最终会填满整个屏幕。(如果内容较少,则会包含很多内容)。我希望它的宽度大小保持相同(即整个屏幕宽度减去左右边距),并且根据其中的内容换行高度(增加/减少)。
这是我当前的 Toast Xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/jtoast_bg"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<ImageView
android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:scaleType="fitStart"
android:src="@drawable/menu_info">
</ImageView>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:textSize="14sp"
android:gravity="center"
android:text="toast text"
android:textColor="#FFF">
</TextView>
它被填充为:
View layout = inflater.inflate(R.layout.jtoast,(ViewGroup)activity.findViewById(R.id.toast_root));
..
toast.setGravity(Gravity.BOTTOM, 40, 40);
..
最佳答案
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#990099"
android:layout_gravity="center"
>
<View
android:layout_width="25dp"
android:layout_height="fill_parent"
/>
<LinearLayout
android:id="@+id/toast_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#999900"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="5dp"
>
<ImageView
android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:scaleType="fitStart"
android:gravity="center"
android:src="@android:drawable/ic_btn_search"
>
</ImageView>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:textSize="14sp"
android:text="toast text"
android:textColor="#FFF"
android:layout_weight="1"
android:gravity="center"
android:background="#ff00"
/>
</LinearLayout>
<View
android:layout_width="25dp"
android:layout_height="fill_parent"
/>
</LinearLayout>
关于android - 自定义 Toast 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9553995/