最小化代码量(烘烤)

时间:2015-01-27 04:00:25

标签: android eclipse toast

我有一堆敬酒代码,这让我的项目文件变得拥挤。有谁知道如何通过使用抽象类来减少它?继承我的吐司代码块

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_example1,
            (ViewGroup) findViewById(R.id.toast_layout_root));
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);   
        toast.setView(layout);
        toast.show();
谢谢你。我希望这个问题可以帮助未来的开发人员。

更新这是我的工作

public abstract class Utility {

public static void makeToast(Context context, int s, int layoutId)
{
    try
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layoutId, null);
        Toast toast = new Toast(context);
    //  TextView text = (TextView) layoutId.findViewById(R.id.textView1);
        text.setText(s);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();

    }
    catch(Exception ex)
    {
        Log.e("UTIL", "Caught exception while attempting to create 
              alertdialog");
        ex.printStackTrace();

我在如何设置textview文本方面遇到困难,以便我可以在一个布局中使用一堆吐司

2 个答案:

答案 0 :(得分:0)

请执行以下操作:

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.demo.R;

public class CustomToast extends LinearLayout {
private Toast toast;
private LayoutInflater inflater;

public CustomToast(Context context) {
    super(context);
    toast = new Toast(getContext());
    inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


public void displayToast(String message){

    View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_linear_toast));
    TextView text = (TextView) layout.findViewById(R.id.tv_toast_message);
    text.setText(message);

    toast.setGravity(Gravity.BOTTOM, 0, 0);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();
}

public void cancelToast(){
    toast.cancel();
}
}

布局文件:custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_linear_toast"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:background="@color/light_red">

<TextView
    android:id="@+id/tv_toast_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="Custom Toast"
    android:textSize="16sp"
    android:textStyle="bold"
    android:textColor="@android:color/white" />

</LinearLayout>

调用此类及其方法,并在需要时显示/取消吐司。

答案 1 :(得分:-1)

Call this toast method and pass the parameters to display the toast.

void toast(int layoutname,int layoutid,int gravity){
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(layoutname,
        (ViewGroup) findViewById(layoutid));
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(gravity, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);   
    toast.setView(layout);
    toast.show();
}