[英]Adding custom layout in an AlertDialog
How to I use my xml as a layout for my dialog? 如何使用我的xml作为对话框的布局? This class is used to show a dialog but the problem is i want to set my own layout. 这个类用于显示对话框,但问题是我想设置自己的布局。
public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
final Dialog dialog = new Dialog(mContext);
dialog.setTitle("Rate " + APP_TITLE);
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(mContext);
tv.setText("If you enjoy using " + APP_TITLE + ", please take a moment to rate it. Thanks for your support!");
tv.setWidth(240);
tv.setPadding(4, 0, 4, 10);
ll.addView(tv);
Button b1 = new Button(mContext);
b1.setText("Rate " + APP_TITLE);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
dialog.dismiss();
}
});
ll.addView(b1);
Button b2 = new Button(mContext);
b2.setText("Remind me later");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
ll.addView(b2);
Button b3 = new Button(mContext);
b3.setText("No, thanks");
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
}
});
ll.addView(b3);
dialog.setContentView(ll);
dialog.show();
}
I want it to have 3 buttons and a picture. 我希望它有3个按钮和一张图片。 Is it possible to do so? 有可能这样做吗?
You can set custom layout to your dialog like below: 您可以将自定义布局设置为对话框,如下所示:
Create a custom layout file: 创建自定义布局文件:
custom.xml custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"/>
</RelativeLayout>
Then in your activity: 然后在你的活动中:
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text1);
text.setText("Text view 1");
TextView text = (TextView) dialog.findViewById(R.id.text2);
text.setText("Text view 2");
dialog.show();
DialogFragment is now the canonical way to display overlays; DialogFragment现在是显示叠加层的规范方式; using Dialog directly is considered bad practice 直接使用Dialog被认为是不好的做法
Usage 用法
Custom View 自定义视图
<!-- fragment_edit_name.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:orientation="vertical" >
<TextView
android:id="@+id/lbl_your_name" android:text="Your name"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="@+id/txt_your_name"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionDone" />
</LinearLayout>
and DialogFragment will be 和DialogFragment将
import android.support.v4.app.DialogFragment;
// ...
public class EditNameDialogFragment extends DialogFragment {
private EditText mEditText;
public EditNameDialogFragment() {
// Empty constructor is required for DialogFragment
// Make sure not to add arguments to the constructor
// Use `newInstance` instead as shown below
}
public static EditNameDialogFragment newInstance(String title) {
EditNameDialogFragment frag = new EditNameDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_edit_name, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Get field from view
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
// Fetch arguments from bundle and set title
String title = getArguments().getString("title", "Enter Name");
getDialog().setTitle(title);
// Show soft keyboard automatically and request focus to field
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
and showing the dialog in an Activity: 并在Activity中显示对话框:
public class DialogDemoActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showEditDialog();
}
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialogFragment editNameDialogFragment = EditNameDialogFragment.newInstance("Some Title");
editNameDialogFragment.show(fm, "fragment_edit_name");
}
}
Custom alert dialog 自定义警报对话框
* using create custom alert dialog box*/
private void Multiple_spinner_alert(int position) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.multiple_spinner_recycler_layout);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
Window window = dialog.getWindow();
final RecyclerView recycler_spinner = (RecyclerView) dialog.findViewById(R.id.recycler_spinner);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recycler_spinner.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true));
recycler_spinner.setLayoutManager(layoutManager);
mAdapter = new MultipleSpinnerRecyclerAdapter(context, getSetList,listPK_ProviderTypeOptions_ID);
recycler_spinner.setAdapter(mAdapter);
final TextView alert_tv_ok=(TextView)dialog.findViewById(R.id.alert_tv_ok);
onChangeSelectedReceivers();
alert_tv_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { {
// do your code
dialog.dismiss();
}
});
dialog.show();
}
it helps you 它可以帮助你
In your layout resource file make sure the android:layout_width is 在您的布局资源文件中,确保android:layout_width是
android:layout_height = "wrap_content"
and not 并不是
android:layout_height = "match_parent"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.