在我的应用程序中,我有几个对话框片段。 我想要的是这个:
当一个特定的对话框片段打开并且发生旋转时,我想阻止该特定片段的屏幕旋转。
另外,我希望其他片段在屏幕旋转时正常。
这是对话框片段的java代码,我想停止屏幕旋转:
public class DialogDelete extends DialogFragment implements View.OnClickListener {
TextView textViewNaslov, textViewNote;
ImageView mBtnClose, mBtnReturn, mBtnDelete;
private RealmResults<Drop> mRealmResolts;
private Realm mRealm;
private Calendar calendar;//pravimo objekt kalendara
private BucketPickerView_Unselected mBucketNoteTimeNonSelected; //ovo nam je drugi bucket pickerview kojeg ne mozemo mjenjati
private Bundle argumetns;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogTheme); //dodajemo temu koju smo napravili preko editora
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_delete, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mBtnClose = (ImageButton) view.findViewById(R.id.btn_close_note_delete);
mBtnReturn = (ImageButton) view.findViewById(R.id.dialog_btn_return_delete);
mBtnDelete = (ImageView) view.findViewById(R.id.dialog_btn_delete);
textViewNaslov = (TextView) view.findViewById(R.id.dialog_text_naslov_delete);
textViewNote = (TextView) view.findViewById(R.id.dialog_text_note_delete);
mBucketNoteTimeNonSelected = (BucketPickerView_Unselected) view.findViewById(R.id.bpv_date_in_note_unselected_deleted);
argumetns = getArguments(); // ovo je bundle
int position = argumetns.getInt("POSITION");
textViewNaslov.setText(mRealmResolts.get(position).getWhat());
textViewNote.setText(mRealmResolts.get(position).getWhat_note());
long vrijeme = mRealmResolts.get(position).getWhen();//dohvacamo trenutno vrijeme iz realma
calendar = Calendar.getInstance();// dohvacamo objekt kalendar sa masu podataka a medju njima je i DATE, YEAR, MONTH...
calendar.setTimeInMillis(vrijeme); //postavljamo da je vrijeme ovog kalendar objekta ono koje smo dohvatili iz realma
int date = calendar.get(Calendar.DATE); //dohvacamo iz kalendar bjekta date
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
mBucketNoteTimeNonSelected.update(date, month, year, 0, 0, 0);//ovo nam je za onaj drugi bucket picker view sto ne mozemo da ga mjenajmo
mBtnClose.setOnClickListener(this);
mBtnReturn.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
//Pozivamo iz activity prilikom klika i saljemo ove podatke koje koristimo onda ovdje
public void setChangeDeleteListener(ChangeNoteListener mChangeNoteListener, RealmResults<Drop> realmResults, Realm realm) {
mRealmResolts = realmResults;
mRealm = realm;
}
@Override
public void onClick(View v) {
int id = v.getId();
int position = argumetns.getInt("POSITION");
switch (id) {
case R.id.btn_close_note_delete:
dismiss();
break;
case R.id.dialog_btn_return_delete:
mRealm.beginTransaction();
mRealm.copyToRealm(mRealmResolts.get(position)).setDeleted(false);
mRealm.commitTransaction();
Log.d("Petarkaracic", "onClick: Ovo je u dilaog delete ");
dismiss();
break;
case R.id.dialog_btn_delete:
mRealm.beginTransaction();
mRealmResolts.deleteFromRealm(position);
mRealm.commitTransaction();
Log.d("Petarkaracic", "onClick: Ovo je u dilaog delete ");
dismiss();
break;
}
}
}
答案 0 :(得分:3)
在DialogFragment
类中设置要锁定屏幕旋转的以下标记
@Override public void onResume() {
super.onResume();
//lock screen to portrait
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override public void onPause() {
super.onPause();
//set rotation to sensor dependent
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
答案 1 :(得分:1)
在您的片段中添加此行
Settings.System.putInt( getActivity().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
//change 0 to 1 to enable screen rotation
您可能需要添加权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
您可能需要为此
添加运行时权限