Custom_toast和custom_toast_layout_id未解决

时间:2015-12-04 09:57:10

标签: android toast

我正在尝试运行一个Toast应用程序。其中一个按钮显示正常的Toast消息,第二个按钮显示带图像的自定义Toast(ic_launcher)和一些文本。

正常的吐司工作正常,没有插入第二个按钮代码。对于定制吐司。 1.custom_layout和2. custom_toast_layout_id无法识别,应用程序显示错误。

这是xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/ivImage"
android:layout_marginRight="5dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"
android:id="@+id/button"
android:layout_below="@+id/tv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Again"
android:id="@+id/button2"
android:layout_marginTop="56dp"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/tvToast"
android:textColor="#000"/>


</RelativeLayout>

这是我的活动:     包com.example.rahulshukla.myapplication;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;


public class MainActivity extends Activity {
TextView helloText;
Button press, press2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
press.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"This is a Toast",  Toast.LENGTH_LONG).show();
}
});
press2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout_id));
ImageView image = (ImageView)findViewById(R.id.ivImage);
image.setImageResource(R.drawable.ic_launcher);

TextView toastText = (TextView)findViewById(R.id.tvToast);
toastText.setText("Button is clicked");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}

private void initialize() {
helloText = (TextView)findViewById(R.id.tv1);
press = (Button)findViewById(R.id.button);
press2 = (Button)findViewById(R.id.button2);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}

1 个答案:

答案 0 :(得分:1)

在这里添加ID。设置ID android:id="@+id/custom_toast_layout_id"&amp;设置此布局名称custom_toast.xml。然后清除-Rebuild-Restart

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/custom_toast_layout_id">