问题
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