<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:background="@color/colorWhite">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add_album_title"
android:shadowColor="@color/colorWhite"
android:text="Enter the Album Name"
android:textSize="20sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/new_album_name"
android:textSize="25sp"
android:hint="Album name"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/add_new_album_button"
android:text="Add"
android:textAllCaps="false"/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/cancel_new_album_button"
android:text="Cancel"
android:textAllCaps="false"/>
</LinearLayout>
</LinearLayout>
我将此代码作为布局。 我想在我的Activity中将其设置为对话框。但是,当我运行它只是看起来 太平了我是Android初学者,没有太多的造型知识。 有人可以帮我设置对话框的样式。
这是我实现它的代码。我刚刚评论了这些按钮 因为现在我的重点是造型并使其更具吸引力。
public class AddAlbumDialog extends DialogFragment {
Button addButton,cancelButton;
EditText newAlbumName;
TextView title;
Context context;
public AddAlbumDialog() {
//Empty Constructor
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setGravity(Gravity.CENTER);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setCancelable(false);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.add_album_dialog_fragment,null));
// Add action buttons
// .setPositiveButton(R.string.add_album, null)
// @Override
// public void onClick(DialogInterface dialog, int which) {
// Dialog d = (Dialog) dialog;
//
// EditText newAddAlbum = (EditText) d.findViewById(R.id.new_album_name);
// newAddAlbum.requestFocus();
// if (newAddAlbum.getText().toString().trim().isEmpty()) {
// Toast.makeText(getActivity(), "Empty Name Cannot Add", Toast.LENGTH_SHORT).show();
//
// } else {
// mListener.onDialogPositiveClick(AddAlbumDialog.this, newAddAlbum);
// }
// }
// })
//Add Negative Button
// .setNegativeButton(R.string.cancel_dialog, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getActivity(), "Cancel Called Name", Toast.LENGTH_SHORT).show();
// mListener.onDialogNegativeClick(AddAlbumDialog.this);
// dismiss();
// }
// });
return builder.create();
}
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface AddAdlbumListener {
public void onDialogPositiveClick(DialogFragment dialog,EditText newAlbumName);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
AddAdlbumListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (AddAdlbumListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
}
在主要活动中,我使用此代码进行显示。
AddAlbumDialog addAlbumDialog=new AddAlbumDialog();
addAlbumDialog.show(getSupportFragmentManager(),"Custom Dialog");
如果有人能提供帮助,我将非常感激。
答案 0 :(得分:2)
如果你想用漂亮的外观制作自定义对话框,那么你需要设置对话框的样式和主题。
A style是一组属性,用于指定视图或窗口的外观和格式。
我刚刚给你发了一些样式的例子,你可以通过下面的行把它设置到你的对话框中:
Dialog dialog = new Dialog(YourActivity.this, R.style.MyTheme);
<强> style.xml 强>
您需要在style.xml中创建新样式:
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:bottomBright">@color/white</item>
<item name="android:bottomDark">@color/white</item>
<item name="android:bottomMedium">@color/white</item>
<item name="android:centerBright">@color/white</item>
<item name="android:centerDark">@color/white</item>
<item name="android:centerMedium">@color/white</item>
<item name="android:fullBright">@color/orange</item>
<item name="android:fullDark">@color/orange</item>
<item name="android:topBright">@color/blue</item>
<item name="android:topDark">@color/blue</item>
</style>
<style name="MyTheme">
<item name="android:alertDialogStyle">@style/CustomDialogTheme</item>
</style>
这只是一个例子。如果您想了解更多详情,请点击以下链接: http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/
希望它会对你有所帮助。
答案 1 :(得分:1)
public void dialog()
{
final Dialog dialog = new Dialog(HomeActivity.this, R.style.cust_dialog);
dialog.setTitle("Select Content Language");
dialog.setContentView(R.layout.add_album_dialog_fragment);
// dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
Button button=(Button)dialog.findViewById(R.id.textView_camera);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogLanguage();
ll_slide.setVisibility(View.GONE);
dialog.dismiss();
}
});
dialog.show();
}
并在style.xml
中设置样式:
<style name="cust_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowTitleStyle">@style/dialog_title_style</item>
</style>
<style name="dialog_title_style" parent="android:Widget.TextView">
<item name="android:background">@color/status_bar</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">20sp</item>
<item name="android:padding">10dp</item>
<item name="android:gravity">center_horizontal|center_vertical</item>
</style>