Android 屏幕方向示例含代碼

    Android 屏幕方向示例

    屏幕方向是activity元素的屬性。愛掏網 - it200.comAndroidActivity的方向可以是縱向、橫向、傳感器、未指定等。愛掏網 - it200.com您需要在AndroidManifest.xml文件中定義它。愛掏網 - it200.com

    語法:

     <activity android:name="package_name.Your_ActivityName"  
          android:screenOrientation="orirntation_type">  
    </activity>  
    

    示例:

    <activity android:name=" example.javatpoint.com.screenorientation.MainActivity"  
         android:screenOrientation="portrait">  
    </activity>
    
    <activity android:name=".SecondActivity"  
         android:screenOrientation="landscape">  
    </activity>
    

    屏幕方向屬性的常見取值如下:

    描述
    未指定 這是默認值。愛掏網 - it200.com在這種情況下,系統選擇方向。愛掏網 - it200.com
    縱向 高度大于寬度
    橫向 寬度大于高度
    傳感器 方向由設備方向傳感器確定。愛掏網 - it200.com

    在這個示例中,我們將創建兩個不同屏幕方向的Activity。愛掏網 - it200.com第一個Activity(MainActivity)將是“縱向”方向,第二個Activity(SecondActivity)將是“橫向”方向類型。愛掏網 - it200.com

    activity_main.xml

    <?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>  
    

    Activity類

    package example.javatpoint.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);
        }
    }
    

    activity_second.xml

    <?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>  
    

    SecondActivity類

    package example.javatpoint.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

    在AndroidManifest.xml文件的activity中添加screenOrientation屬性并提供其屏幕方向。愛掏網 - it200.com在這個例子中,我們為MainActivity提供”portrait”方向,為SecondActivity提供”landscape”方向。愛掏網 - it200.com

    <?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
        package="example.javatpoint.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.javatpoint.com.screenorientation.MainActivity"  
                android:screenOrientation="portrait">  
                <intent-filter>  
                    <action android:name="android.intent.action.MAIN" />  
    
                    <category android:name="android.intent.category.LAUNCHER" />  
                </intent-filter>  
            </activity>  
            <activity android:name=".SecondActivity"  
                android:screenOrientation="landscape">  
            </activity>  
        </application>  
    
    </manifest>  
    

    輸出:

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 日韩精品一区二区三区色欲AV | 亚洲国产AV无码一区二区三区| 男插女高潮一区二区| 中文字幕VA一区二区三区| 无码精品视频一区二区三区| 国产福利91精品一区二区| 白丝爆浆18禁一区二区三区 | 中文字幕一区日韩精品| 国产午夜精品片一区二区三区| 亚洲一区视频在线播放| 日韩a无吗一区二区三区| 国产91精品一区二区麻豆网站| 91国在线啪精品一区| 无码精品久久一区二区三区| 在线日韩麻豆一区| 国产福利微拍精品一区二区 | 国产91精品一区二区麻豆亚洲| 国产精品av一区二区三区不卡蜜| 男女久久久国产一区二区三区| 国产成人AV一区二区三区无码| 熟女性饥渴一区二区三区| 亚洲一区二区三区在线视频 | 国产精品资源一区二区| 亚洲熟女www一区二区三区| 在线播放一区二区| 亚洲AV无一区二区三区久久| 久久久国产精品一区二区18禁| 性色AV一区二区三区| 久久婷婷色一区二区三区| 国产婷婷色一区二区三区| 中文字幕日本精品一区二区三区| 亚洲AV日韩精品一区二区三区| 亚洲日韩国产一区二区三区| 国产精品乱码一区二区三区| 东京热无码av一区二区| 中文字幕精品亚洲无线码一区应用| 国产美女精品一区二区三区| 亚洲av综合av一区| 亚洲一区电影在线观看| 亚洲中文字幕乱码一区| 理论亚洲区美一区二区三区|