带有 ImageView 和 TextView 的 AlertDialog
AlertDialog with ImageView and TextView
我想用 ImageView 和 TextView 制作一个 AlertDialog。
我写了这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/imgCustomToast"
android:layout_width="170dp"
android:layout_height="220dp"
android:background="@drawable/ycp"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/txtCustomToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:textSize="20sp"/>
</LinearLayout>
主要活动:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate
{
AlertDialog.Builder alertadd = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.From(this);
View view = factory.Inflate(Resource.Layout.sample, null);
alertadd.SetView(view);
alertadd.SetPositiveButton("To Close", (senderAlert, args) =>
{
Toast.MakeText(this, "Closed", ToastLength.Short).Show();
});
alertadd.Show();
};
}
}
我想在 MainActivity 中更改文本视图的大小、字体和文本:
TextView textView = FindViewById<TextView>(Resource.Id.txtCustomToast);
string str = "sample text";
textView.Text = str;
Typeface typeP = Typeface.CreateFromAsset(this.Assets, "fonts/BLOTUS.TTF");
textView.SetTypeface(typeP, TypefaceStyle.Normal);
textView.SetTextSize(Android.Util.ComplexUnitType.Sp, 18);
但是我看到这个错误:
System.NullReferenceException
如何以编程方式更改文本视图的大小、字体和文本?
您访问TextView
的方式不对,
更改此行
TextView textView = FindViewById<TextView>(Resource.Id.txtCustomToast);
至此
TextView textView = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
您需要使用 Alert Dialog
的实例找到您的 view
,如下所示。
TextView textView = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
View view = factory.Inflate(Resource.Layout.sample, null);
TextView textView = view.findViewById<TextView>(Resource.Id.txtCustomToast);
string str = "sample text";
textView.setText(str);
调用此方法,显示 AlertDialog
和 ImageView
和 TextView
private void showDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView imageView = new ImageView(context);
TextView textView = new TextView(context);
linearLayout.addView(imageView);
linearLayout.addView(textView);
builder.setCancelable(false);
builder.setView(linearLayout);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//ok
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// negative button logic
}
});
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
}
我想用 ImageView 和 TextView 制作一个 AlertDialog。 我写了这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/imgCustomToast"
android:layout_width="170dp"
android:layout_height="220dp"
android:background="@drawable/ycp"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/txtCustomToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:textSize="20sp"/>
</LinearLayout>
主要活动:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate
{
AlertDialog.Builder alertadd = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.From(this);
View view = factory.Inflate(Resource.Layout.sample, null);
alertadd.SetView(view);
alertadd.SetPositiveButton("To Close", (senderAlert, args) =>
{
Toast.MakeText(this, "Closed", ToastLength.Short).Show();
});
alertadd.Show();
};
}
}
我想在 MainActivity 中更改文本视图的大小、字体和文本:
TextView textView = FindViewById<TextView>(Resource.Id.txtCustomToast);
string str = "sample text";
textView.Text = str;
Typeface typeP = Typeface.CreateFromAsset(this.Assets, "fonts/BLOTUS.TTF");
textView.SetTypeface(typeP, TypefaceStyle.Normal);
textView.SetTextSize(Android.Util.ComplexUnitType.Sp, 18);
但是我看到这个错误:
System.NullReferenceException
如何以编程方式更改文本视图的大小、字体和文本?
您访问TextView
的方式不对,
更改此行
TextView textView = FindViewById<TextView>(Resource.Id.txtCustomToast);
至此
TextView textView = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
您需要使用 Alert Dialog
的实例找到您的 view
,如下所示。
TextView textView = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
View view = factory.Inflate(Resource.Layout.sample, null);
TextView textView = view.findViewById<TextView>(Resource.Id.txtCustomToast);
string str = "sample text";
textView.setText(str);
调用此方法,显示 AlertDialog
和 ImageView
和 TextView
private void showDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView imageView = new ImageView(context);
TextView textView = new TextView(context);
linearLayout.addView(imageView);
linearLayout.addView(textView);
builder.setCancelable(false);
builder.setView(linearLayout);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//ok
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// negative button logic
}
});
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
}