Place a button inside DialogFragment's title

佐手、 提交于 2020-01-15 09:45:52

问题


I show a DialogFragment from another DialogFragment. I need to set title and a button just next to it. I don't want to reimplement the theme and create a custom view inside DialogFragment's content view instead of dialog title (because it's error-prone and time wasting). Is it even possible? I tried many API functions, AlertBuilder, ActionBar, this and that, still didn't found anything that fits my needs.


回答1:


Try something like this:

// Title
final int titleId = getResources().getIdentifier("alertTitle","id","android");
TextView title = (TextView) popup.findViewById(titleId);
title.setText("My new title");

// Title's parent layout
ViewGroup viewGroup = (ViewGroup) title.getRootView();

// Button
Button button = new Button(this);
button.setText("A Button");
viewGroup.addView(button);

Note: You may need to adjust the button and title objects' LayoutParams.




回答2:


An update. This can be achieved by calling setCustomTitle on AlertDialog.Builder class. Available since api level 1, and do not require a bunch of code.

Example:

AlertDialog.Builder(getActivity()).setCustomTitle(getActivity().getLayoutInflater().inflate(R.layout.my_custom_view, viewGroup)).create()



来源:https://stackoverflow.com/questions/25530127/place-a-button-inside-dialogfragments-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!