screenOrientation 是Activity元素的属性。 Android Activity的方向可以是纵向(portrait),横向(landscape),传感器(sensor),未指定(unspecified)等。您需要在AndroidManifest.xml文件中定义它。
语法:
<activity android:name="package_name.Your_ActivityName" android:screenorientation="orirntation_type"></activity>
示例:
<activity android:name="example.learnfk.com.screenorientation.MainActivity" android:screenOrientation="portrait"></activity>
<activity android:name=".SecondActivity" android:screenOrientation="landscape"></activity>
screenOrientation属性的公共值如下:
值 | 描述 |
---|---|
unspecified | 它是默认值。在这种情况下,系统选择方向。 |
portrait | 纵向 |
landscape | 横向 |
sensor | 方向由器件方向传感器确定。 |
在此示例中,无涯教程将创建两个不同屏幕方向的Activity。第一种Activity将是"纵向(portrait)"定向和第二个Activity,作为"landscape(横向)"定向类型。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="example.javatpoint.com.screenorientation.MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="112dp" android:onClick="onClick" android:text="Launch next activity" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.612" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText1" app:layout_constraintVertical_bias="0.613" /> <TextView android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="124dp" android:ems="10" android:textSize="22dp" android:text="This activity is portrait orientation" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.502" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
package example.learnfk.com.screenorientation; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button1); } public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="example.javatpoint.com.screenorientation.SecondActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="180dp" android:text="this is landscape orientation" android:textSize="22dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.502" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
package example.learnfk.com.screenorientation; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } }
在AndroidManifest.xml文件中,在Activity中添加screenOrientation属性并提供其方向。
<!--?xml version="1.0" encoding="utf-8"?--> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.learnfk.com.screenorientation"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="example.learnfk.com.screenorientation.MainActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN"></action> <category android:name="android.intent.category.LAUNCHER"></category> </intent-filter> </activity> <activity android:name=".SecondActivity" android:screenOrientation="landscape"></activity> </application> </manifest>
运行上面代码输出
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)