我希望平板电脑能够显示肖像和景观(sw600dp或更大),但手机仅限于肖像。我找不到任何有条件地选择方向的方法。有什么建议吗?
当前回答
我在toMainActivity.java文件中添加了这个代码部分。它对我的案子起作用了。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleOrientationConfiguration();
}
private void handleOrientationConfiguration() {
if (isTablet()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
private boolean isTablet() {
Configuration configuration = this.getContext().getResources()
.getConfiguration();/*from www .j ava 2 s. c o m*/
return (configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
其他回答
不幸的是,使用setRequestedOrientation(…)方法会导致活动重新启动,所以即使你在onCreate方法中调用这个,它也会经历活动生命周期,然后它会在请求的方向上重新创建相同的活动。所以在@Brian Christensen的回答中,你应该考虑到活动代码可能会被调用两次,这可能会产生不良影响(不仅是视觉上,而且在网络请求、分析等方面也会产生不良影响)。
此外,在清单中设置configChanges属性在我看来是一个很大的权衡,这可能会花费大量的重构成本。Android开发者不建议改变这个属性。
最后,尝试以不同的方式设置screenOrientation(以避免重新启动问题)是不可能的,静态不可能,因为静态manifest不能被改变,编程上只可能在已经启动的活动中调用该方法。
总结:在我看来,@Brian Christensen的建议是最好的权衡,但要注意重新启动活动的问题。
其他的解决方案对我都不起作用。在对话和娱乐方面,我仍然有一些奇怪的方向问题。我的解决方案是扩展活动,迫使它作为肖像在manifest。
例子:
public class MainActivityPhone extends MainActivity {}
manifest.xml:
<activity
android:screenOrientation="portrait"
android:name=".MainActivityPhone"
android:theme="@style/AppTheme.NoActionBar" />
在溅屏活动中:
Intent i = null;
boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
if (!isTablet)
i = new Intent(this, MainActivityPhone.class);
else
i = new Intent(this, MainActivity.class);
startActivity(i);
你应该在你的manifest中声明你的屏幕朝向,值为'nosensor':
方向的确定不需要参考物理方向传感器。传感器被忽略,所以显示器不会根据用户移动设备的方式旋转。
然后旋转将是“自然的”设备行为(手机:纵向/平板电脑:横向)。
优点是你可以混合每个活动的不同旋转,例如,如果你有一个显示数据库记录的活动,那么你可以将这个活动的方向设置为“fullUser”,而其他活动则使用“nosensor”。"fullUser"活动将根据用户选择旋转(启用旋转:使用传感器,否则使用用户偏好)。
<activity android:name=".scanner.ScannerPropertiesActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/title_activity_scanner_properties"
android:screenOrientation="${screenOrientation}"/>
${screenOrientation}是一个manifest占位符,所以我可以很容易地切换所有的活动,我希望测试之间的所有取向行为使用这个:
//module's build.gradle
defaultConfig {
//...
manifestPlaceholders = [applicationName:appName,screenOrientation:'nosensor']
//...
}
如果你需要更复杂的功能(例如:只允许平板电脑屏幕旋转,并将手机方向固定为竖屏),那么你应该使用locked:
android: screenOrientation = "锁定" 我注意到,如果你强迫方向为纵向,而传感器发送的手机是在横向旋转,你的活动将开始在横向,然后旋转到纵向一旦你的代码被执行,几乎任何其他方向。 锁定保持以前的旋转,所以你可能已经锁定在纵向从你的上一个活动。那么你必须知道什么时候一个设备是平板电脑或手机:为了做到这一点,我建议你使用这个:
//normal tablet_detection file content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="isTablet" type="bool">false</item>
</resources>
//sw600dp tablet_detection file content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="isTablet" type="bool">true</item>
</resources>
//Now in your code you can detect if it's a tablet or not by calling following function in your Activity's **onCreate** and apply screen orientation by code:
protected void updateScreenOrientation(){
boolean isTablet =context.getResources().getBoolean(R.bool.isTablet);
//And select the orientation you wish to set:
int defaultOrientation=isTablet? ActivityInfo.SCREEN_ORIENTATION_FULL_USER:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
if(getRequestedOrientation()== ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
//Apply this only on Activities where you set the orientation to 'locked' in the manifest
this.setRequestedOrientation(defaultOrientation);
}
}
根据Ginny的回答,我认为最可靠的方法如下:
如本文所述,在资源sw600dp中放入一个布尔值。它必须有前缀sw,否则它将无法正常工作:
在res / values-sw600dp / dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">true</bool>
</resources>
在res /价值/ dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
</resources>
然后创建一个方法来检索布尔值:
public class ViewUtils {
public static boolean isTablet(Context context){
return context.getResources().getBoolean(R.bool.isTablet);
}
}
和一个base activity来扩展你想要的行为:
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!ViewUtils.isTablet(this)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
因此每个活动都将扩展BaseActivity:
public class LoginActivity extends BaseActivity //....
重要:即使你从BaseActivity扩展,你必须添加行android:configChanges="orientation|screenSize"到每个活动在你的AndroidManifest.xml:
<activity
android:name=".login.LoginActivity"
android:configChanges="orientation|screenSize">
</activity>
建议的方法是不锁定任何窗口维度的方向,但您可以通过遵循这里的指南来实现。
在步骤中,要做的第一件事是解锁舱单上的方向
<activity
android:name=".MyActivity"
android:screenOrientation="fullUser">
接下来,你可以使用Jetpack窗口管理器来确定应用程序在屏幕上有多少空间:
/** Determines whether the device has a compact screen. **/
fun compactScreen(): Boolean {
val screenMetrics = WindowMetricsCalculator
.getOrCreate()
.computeMaximumWindowMetrics(this)
val shortSide = min(screenMetrics.bounds.width(),
screenMetrics.bounds.height())
return shortSide / resources.displayMetrics.density < 600
}
最后,当空间变小时,锁定方向,通常在手机上:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestedOrientation = if (compactScreen())
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else
ActivityInfo.SCREEN_ORIENTATION_FULL_USER
...
}
推荐文章
- 为什么我在使用adb时访问数据文件夹被拒绝?
- 如何确定Android设备的GPS是否已启用
- 没有找到与给定名称匹配的资源:attr 'android:keyboardNavigationCluster'。更新到支持库26.0.0时
- 禁用在android中打开导航抽屉的滑动手势
- Android:允许平板电脑竖屏和横屏,但强制手机竖屏?
- 如何使布局与视图填充剩余的空间?
- 字符串资源新行/n不可能?
- 如何点击或点击一个TextView文本
- Android房间-简单的选择查询-不能访问数据库在主线程
- Android: ScrollView vs NestedScrollView
- 错误:无法运行mksdcard SDK工具
- 未附加到窗口管理器的视图崩溃
- 将焦点设置为EditText
- 如何把空格字符放入XML字符串名?
- 我可以设置“android:layout_below”在运行时编程?