如何在我的Android应用中禁用某些视图的横向模式?
我正在尝试为我的应用程序强制"纵向"模式,因为我的应用程序绝对不是为"横向"模式设计的.
在阅读了一些论坛后,我在清单文件中添加了这些行:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:screenOrientation="portrait">
Run Code Online (Sandbox Code Playgroud)
但它不适用于我的设备(HTC Desire).它从"纵向"lo"横向"切换,忽略清单文件中的行.
在更多论坛阅读之后,我尝试在我的清单文件中添加它:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
Run Code Online (Sandbox Code Playgroud)
这个函数在我的activity类中:
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Run Code Online (Sandbox Code Playgroud)
但同样,没有运气.
我在Android 8.0 Oreo java.lang.IllegalStateException中从联系簿中检索联系人时遇到问题:只有全屏不透明活动可以请求方向
我试图通过电话联系簿获取我的活动中的联系人,它对Lollipop,Marshmallow,Nougat等非常有用,但它会给我这样的奥利奥错误,请帮助我.我的代码如下.
演示代码: -
private void loadContacts() {
contactAsync = new ContactLoaderAsync();
contactAsync.execute();
}
private class ContactLoaderAsync extends AsyncTask<Void, Void, Void> {
private Cursor numCursor;
@Override
protected void onPreExecute() {
super.onPreExecute();
Uri numContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] numProjection = new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
if (android.os.Build.VERSION.SDK_INT < 11) {
numCursor = InviteByContactActivity.this.managedQuery(numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
} else {
CursorLoader cursorLoader = new CursorLoader(InviteByContactActivity.this, numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE …Run Code Online (Sandbox Code Playgroud) 我正在开发基于Android的移动应用程序minSdkVersion=15.我想支持平板电脑的两种方向,只支持智能手机的肖像.一切都像魅力一样,但我遇到了一个让我发疯的小虫子.当智能手机处于横向模式并尝试触发新活动时,它会以横向模式打开一段时间,然后自动切换到纵向.我的每个活动都扩展了一个GeneralActivity类:
public class GeneralActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If smartphone lock orientation to portrait
if (!Helper.isTablet(this.getApplicationContext())){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我检测到具有此功能的平板电脑
public class Helper {
public static boolean isTablet(Context context){
Configuration config = context.getResources().getConfiguration()
return config.smallestScreenWidthDp >= 600;
}
}
Run Code Online (Sandbox Code Playgroud)
我选择不在android:screenOrientationManifest.xml中指定,因为这样我就可以支持平板电脑的所有界面方向.我错过了什么吗?
编辑
我决定应用Jonathan在答案中建议的最佳实践,但我所描述的问题仍然存在.这是我在github上的回购:https://github.com/giacmarangoni/Android-Orientation-Test
我制作了Kotlin应用程序,但不希望该应用程序的屏幕旋转。我知道使用Java,我可以把
android:screenOrientation="portrait"
Run Code Online (Sandbox Code Playgroud)
在AndroidMainfest中,但是Kotlin呢?我该怎么做?
我想为我的整个应用程序禁用横向模式.我希望能够在没有screenOrientation为我的清单中的每个活动设置肖像的情况下这样做.有可能用一个属性来做吗?