如何在我的Android应用中禁用某些视图的横向模式?
我在后台线程(我使用AsyncTask)下载了一些来自互联网的数据,并在下载时显示进度对话框.方向更改,Activity重新启动,然后我的AsyncTask完成 - 我想关闭progess对话框并启动一个新的Activity.但是调用dismissDialog有时会抛出一个异常(可能是因为Activity被销毁而且还没有启动新的Activity).
处理此类问题的最佳方法是什么(从后台线程更新UI即使用户更改方向也能正常工作)?谷歌有人提供了一些"官方解决方案"吗?
我的主要活动有一些代码,可以进行一些不应该被中断的数据库更改.我正在另一个线程中进行繁重的工作,并使用我设置为不可取消的进度对话框.但是,我注意到如果我旋转手机,它会重新启动对正在运行的进程非常不好的活动,并且我会收到强制关闭.
我想要做的是以编程方式禁用屏幕方向更改,直到我的进程完成,此时方向更改已启用.
在我的应用程序中我有TextView和EditText.两者都有数据.当屏幕方向更改EditText剩余数据时,TextView数据将被清除.
有人可以帮助我找到保留数据的方法TextView吗?
android onconfigurationchanged android-layout android-orientation
public class MainActivity extends Activity implements MainMenuFragment.OnMainMenuItemSelectedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
// add menu fragment
MainMenuFragment myFragment = new MainMenuFragment();
fragmentTransaction.add(R.id.menu_fragment, myFragment);
//add content
DetailPart1 content1= new DetailPart1 ();
fragmentTransaction.add(R.id.content_fragment, content1);
fragmentTransaction.commit();
}
public void onMainMenuSelected(String tag) {
//next menu is selected replace existing fragment
}
Run Code Online (Sandbox Code Playgroud)
我需要并排显示两个列表视图,左边是菜单,右边是内容,默认选择第一个菜单,右边显示内容.显示内容的片段如下
public class DetailPart1 extends Fragment {
ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
ListAdapter adap;
ListView listview;
@Override
public …Run Code Online (Sandbox Code Playgroud) 我正在尝试控制Android相机在肖像应用程序中拍照,但是当我保存图片时,它是在风景中.我用setCameraDisplayOrientation()方法将图像旋转了90个等级 ,但是不起作用.
然后我发现这个职位,但TAG_ORIENTATIONIS 0(不确定).如果我捕获此值并应用旋转值,则也不起作用.
我如何拍摄肖像并以良好的方向保存?
/** Initializes the back/front camera */
private boolean initPhotoCamera() {
try {
camera = getCameraInstance(selected_camera);
Camera.Parameters parameters = camera.getParameters();
// parameters.setPreviewSize(width_video, height_video);
// parameters.set("orientation", "portrait");
// parameters.set("rotation", 1);
// camera.setParameters(parameters);
checkCameraFlash(parameters);
// camera.setDisplayOrientation( 0);
setCameraDisplayOrientation(selected_camera, camera);
surface_view.getHolder().setFixedSize(width_video, height_video);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width_video, height_video);
surface_view.setLayoutParams(lp);
camera.lock();
surface_holder = surface_view.getHolder();
surface_holder.addCallback(this);
surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setPreviewCamera();
} catch (Exception e) {
Log.v("RecordVideo", "Could not initialize the Camera");
return false;
}
return true; …Run Code Online (Sandbox Code Playgroud) 在我的片段中,我按以下方式设置GridLayout:
mRecycler.setLayoutManager(new GridLayoutManager(rootView.getContext(), 2));
所以,我只是想改变2的4,当用户旋转手机/平板电脑.我已经读过了onConfigurationChanged,我试图让它适用于我的情况,但它没有以正确的方式进行.当我旋转手机时,应用程序崩溃了......
你能告诉我如何解决这个问题吗?
以下是我找到解决方案的方法,该方法无法正常工作:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mRecycler.setLayoutManager(new GridLayoutManager(mContext, 4));
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
android android-orientation android-gridlayout android-recyclerview