問題描述
我正在嘗試創建一個警報對話框,頂部有一個圖像標題,底部有 2 個按鈕,我想通過代碼在中間設置文本.但是,我無法在橫幅下方設置文本.我想創建類似于 this link 上的內容.但是,文本總是在圖像上方結束,因為我不知道如何在對話框構建器中引用文本視圖.謝謝
I'm trying to create an alert dialog with an image header across the top, 2 buttons at the bottom and I want to set the text in the middle from code. However, I can't get the set text underneath the banner. I want to create something similar to the one on this link. However, the text always ends up above the image as I don't know how to reference the textview in the dialog builder. Thanks
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.custom_dialog, null))
.setMessage("Message" + message)
.setPositiveButton("Share", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (isNetworkAvailable()) {
if (...) {...}
else {...}
} else {...}
}
})
.setNegativeButton("Shake again!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {}
});
AlertDialog alert = builder.create();
alert.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {}
});
alert.show();
布局xml文件為:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/yo3"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/dialogtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>`
推薦答案
不要用你的方式設置消息.
Don't set the message with the way you do it.
在您的自定義布局中,您必須設置的 textview 是dialogtext"TextView.試試這個……看看我得到了我膨脹的自定義視圖,從那個視圖中我得到了自定義對話框(你之前實際上沒有設置)并設置消息在構建發生后顯示.實際上,您可以擁有任何您想要的自定義視圖,并根據自己的喜好設置視圖的每個元素,甚至可以處理其上的事件
In your custom layout the textview you have to set is the "dialogtext" TextView. Try this...see that i get the custom view that i inflate and from that view i get the custom dialog (which you didn't actualy set before) and set the message to show after the build has taken place. Actually you can have whatever custom view you want and set each element of the view as you prefer or even handle events on it
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_dialog, null);
TextView messageView = (TextView)customView.findViewById(R.id.dialogtext);
builder.setView(customView)
.setPositiveButton("Share", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (isNetworkAvailable()){
if (......;
} else {
......
}
} else {
.......;
}
}
})
.setNegativeButton("Shake again!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
}).setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}
});
messageView.SetText("Message" + message);
AlertDialog alert = builder.create();
alert.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
}
});
alert.show();
這篇關于Android 在警報對話框中設置文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!