我想将图像拉伸到全屏或匹配宽度。以语法形式在吐司中。 目前它所做的只是包装内容。
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.mipmap.ic_launcher);
//imageView.setAdjustViewBounds(true);
//imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
}
public void toast(View v){
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(imageView);
toast.show();
}}
正如大家所见,已经尝试过所有setAdjustViewBounds,FIT XY,FIT CENTER 任何人都可以暗示如何使这成为可能吗?
答案 0 :(得分:0)
您可以通过以下方式创建自定义吐司。您可以使用图像视图而不是文本视图。
custom_toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="@color/design_text_background_color_argb">
<TextView
android:id="@+id/custom_toast_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/activity_title_color"
android:enabled="true"
android:textSize="16dp"
/>
</LinearLayout>
使用以下代码:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.custom_toast_text);
text.setText(error);
if (myInvalidPremiumToast != null)
myInvalidPremiumToast.cancel();
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
答案 1 :(得分:0)
\r\n
// java code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image_4"/>
</LinearLayout>