2015-01-27 2 views
-5

У меня есть куча костей для тостов, что делает мой файл проекта переполненным. Кто-нибудь знает, как уменьшить его, используя абстрактный класс? heres мой блок кодов моего тостаМинимизировать тонны кодов (тостов)

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(); 

спасибо. И я надеюсь, что этот вопрос может помочь будущим разработчикам.

UPDATE вот моя работа

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 текст так, что я могу использовать кучу тостов в одном макете

+0

Каково было бы преимущество абстракции? – mrres1

+0

Я могу свести к минимуму мои коды при использовании методов там. также он помогает сэкономить больше макетов. – user3698267

ответ

0
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(); 
} 
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> 

Назовите этот класс и его методы, чтобы показать/отменить тост, когда это необходимо.

+0

Как я могу изменить параметр метода в 'int', потому что я хочу использовать R.string. – user3698267

+0

см. Мое выше edit – user3698267

+0

Измените text.setText (s) на text.setText (context.getString (s)) ;, также проиндексируйте Textview, иначе он выдает ошибку. –

Смежные вопросы