java 手机纵向,平板横向(Android-布局)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7668367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:55:17  来源:igfitidea点击:

Portrait for phone, landscape for Tablet (Android-Layout)

javaandroidandroid-layoutorientation

提问by rwarner

So I'm making an application for Android and I want to force Landscape orientation for tablets and Portrait orientation for phones. However, it seems as though I can only do an orientation lock from what I've seen which defeats the purpose of wanting two separate orientations for devices.

所以我正在为 Android 制作一个应用程序,我想强制平板电脑横向和手机纵向。但是,似乎我只能根据我所看到的情况进行方向锁定,这违背了为设备提供两个单独方向的目的。

Tablets: Landscape Phones: Portrait

平板电脑:横向手机:纵向

To put it more technical.

说得更有技术含量。

I have a layout in "res/layout-xlarge-land" for landscaping on the tablet and I have the original layout in "res/layout" and I just want to explicitly use layout-xlarge-land for the tablet. Nothing else, essentially ONLY using landscape for xlarge devices.

我在“res/layout-xlarge-land”中有一个布局用于在平板电脑上进行景观美化,而我在“res/layout”中有原始布局,我只想明确地为平板电脑使用 layout-xlarge-land。没有别的,基本上只对 xlarge 设备使用横向。

Thanks!

谢谢!

回答by Bhavya

Setting a particular orientation based on device density may not work because there are phones which have higher densities than tablets.

根据设备密度设置特定方向可能不起作用,因为有些手机的密度高于平板电脑。

What I did was to disable the device's orientation sensor by setting the attribute in the activity tag in the manifest file like this:

我所做的是通过在清单文件的活动标签中设置属性来禁用设备的方向传感器,如下所示:

android:screenOrientation="nosensor"

When you run your app, by default portrait orientation is set for phones and landscape for tablets(and hence it'll select the xml file from layout-xlarge-land). And since you've set an orientation lock, it remains in this orientation.

当您运行您的应用程序时,默认情况下,手机设置为纵向,平板电脑设置为横向(因此它将从 中选择 xml 文件layout-xlarge-land)。并且由于您设置了方向锁定,因此它保持在该方向。

回答by Mr. Messy

You can measure the actual size (in inches) of the device and then programmatically set the orientation using:

您可以测量设备的实际尺寸(以英寸为单位),然后使用以下方法以编程方式设置方向:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

or

或者

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

To get the physical size of the device you can use the code published here.

要获得设备的物理尺寸,您可以使用此处发布的代码。

It is not 100% accurate, but it is good enough to decide what king of device is running the app.

它不是 100% 准确,但足以确定运行应用程序的设备之王。

回答by Yevgeny Simkin

I'm not entirely sure what you're saying, but it would seem that you should just measure the resolution and act accordingly. I mean, how do you know if it's a phone or a tablet OTHER than the resolution being different?

我不完全确定你在说什么,但似乎你应该衡量分辨率并采取相应的行动。我的意思是,除了分辨率不同之外,你怎么知道它是手机还是平板电脑?

回答by Hala.M

for people who used

对于使用过的人

android:screenOrientation="nosensor"

and did not work on it own I used it in addition to this snippet in my base activity

并且没有单独工作 我在我的基本活动中除了这个片段之外还使用了它

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            && (this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) < Configuration.SCREENLAYOUT_SIZE_LARGE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }else if((this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    super.onCreate(savedInstanceState);
}

回答by Dante

set a layout called llTablet on the layout menu in the "layout-large" folder layouts and a llPhone layout on the layout menu in the "layout" folder. By menu am refering to the first layout the user is entering. Then check if you can reference it. If you can't reference llPhone then its a tablet.

在“layout-large”文件夹布局中的布局菜单上设置一个名为 llTablet 的布局,在“layout”文件夹中的布局菜单上设置一个 llPhone 布局。菜单是指用户输入的第一个布局。然后检查您是否可以参考它。如果您不能引用 llPhone,则它是平板电脑。

llPhone = (LinearLayout) findViewById (R.id.llPhone)
if (llPhone == null) {
 tablet = true;
}
else {
 tablet = false;
}