görünümün konumunu değiştirmek istemiyorum rotasyon
Not henüz güncellenmedi. Aradığınız güncellemeleri yakalamak için görünüme bir OnLayoutChangeListener
eklemeniz gerekir. Aşağıdaki örneğe bakınız.
TestDialogFragment.java
public class TestDialogFragment extends DialogFragment {
private static final String TAG = "TestDialogFragment";
View testView;
int[] testViewLocation = {0, 0};
public TestDialogFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_fragment, container);
testView = view.findViewById(R.id.test_view);
testView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Log.d(TAG, "onLayoutChange");
testView.getLocationOnScreen(testViewLocation);
Log.d(TAG, String.format("%s %s", testViewLocation[0], testViewLocation[1]));
}
});
return view;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged");
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d(TAG, "landscape");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Log.d(TAG, "portrait");
}
testView.getLocationOnScreen(testViewLocation);
Log.d(TAG, String.format("%s %s", testViewLocation[0], testViewLocation[1]));
}
}
test_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/test_view"
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_gravity="center"/>
</FrameLayout>
Günlüğü Çıktı
06-24 16:20:05.682 D/TestDialogFragment﹕ onConfigurationChanged
06-24 16:20:05.682 D/TestDialogFragment﹕ portrait
06-24 16:20:05.682 D/TestDialogFragment﹕ 504 601
06-24 16:20:05.852 D/TestDialogFragment﹕ onLayoutChange
06-24 16:20:05.852 D/TestDialogFragment﹕ 84 1021
06-24 16:20:08.695 D/TestDialogFragment﹕ onConfigurationChanged
06-24 16:20:08.695 D/TestDialogFragment﹕ landscape
06-24 16:20:08.695 D/TestDialogFragment﹕ 84 1021
06-24 16:20:08.865 D/TestDialogFragment﹕ onLayoutChange
06-24 16:20:08.865 D/TestDialogFragment﹕ 504 601
06-24 16:20:13.550 D/TestDialogFragment﹕ onConfigurationChanged
06-24 16:20:13.550 D/TestDialogFragment﹕ portrait
06-24 16:20:13.550 D/TestDialogFragment﹕ 504 601
diyoruz? Özellikle _when_ ile ilgili kod 'getLocationOnScreen' kontrol ediyorsunuz. [Bu] (http: // stackoverflow.com/questions/13243914/getlocationonscreen-in-oncreate-just-returns-zeroes) sizinki sorun değil ama benzer bir sebebi varsa merak ediyorum. – mpkuth
Hayır, o böyle değil. Görünüm zaten çizilmiş ve doğru şekilde yerleştirilmiş. Sorun, etkinlik rekreasyonunu devre dışı bırakan 'android: configChanges' ile ilgilidir. Bu durumda, etkinlik yok olmadığından, iletişim kutusu da yok olmaz. İşte bu yüzden görüşün konumu eskisi gibi duruyor. Bu çoğaltılması kolay olduğundan kod gerekmez. –