Android:未使用横向布局

Android:未使用横向布局,android,Android,在我的应用程序中,我有一个主要活动,它设置了一个带有三个不同片段的选项卡式ViewPager,每个片段都有自己的布局和类。我已经创建了每个布局文件的横向版本,并将它们全部放在res/layoutland中。但当我运行应用程序并切换方向时,是否没有使用横向布局 主要活动: package me.zaydbille.utilitywatch; import android.content.Intent; import android.content.res.Configuration; impor

在我的应用程序中,我有一个主要活动,它设置了一个带有三个不同片段的选项卡式ViewPager,每个片段都有自己的布局和类。我已经创建了每个布局文件的横向版本,并将它们全部放在res/layoutland中。但当我运行应用程序并切换方向时,是否没有使用横向布局

主要活动:

package me.zaydbille.utilitywatch;

import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

    // Tab and ViewPager variables
    Toolbar             toolbar;
    ViewPager           pager;
    ViewPagerAdapter    adapter;
    SlidingTabLayout    tabs;
    CharSequence Titles[]={"Coin Flip", "Counter", "Choice"};
    int numTabs = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Creating The Toolbar and setting it as the Toolbar for the activity
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, numTabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent myIntent = new Intent(this, HelpActivity.class);
            startActivity(myIntent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // Counter Fragment helper methods
    public void addCount() {
        Preferences.setIntPref(this, Preferences.getIntPref(this) + 1);
    }

    public void clearCount() { Preferences.setIntPref(this, 0); }

    public int getCount() { return Preferences.getIntPref(this); }

    // Choice Fragment helper methods
    public void saveList(ArrayList<String> list){ Preferences.saveList(this, list); }
    public ArrayList<String> getList() { return Preferences.getList(this); }
}
片段1的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/ColorPrimaryLight">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="false"
        android:layout_alignParentBottom="false"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginTop="200dp">

        <ImageView
            android:id="@+id/coin_spinning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/coin_spin_heads"
            android:contentDescription="@string/coinDescription" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/flipButtonText"
            android:id="@+id/flipButton"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>

</RelativeLayout>

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="me.zaydbille.utilitywatch" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenLayout|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".HelpActivity"
            android:label="@string/title_activity_help"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="me.zaydbille.utilitywatch.MainActivity" />
        </activity>
    </application>

</manifest>


当您使用
android:configChanges=“orientation | screenLayout | screenSize”
时,您是说您不希望系统在发生这些配置更改时执行任何默认行为。这包括更改任何布局删除该行或实际上删除。

只需从AppManifest.xml中删除这段代码:

android:configChanges="orientation|screenLayout|screenSize"
这将禁用活动以更改其方向。去掉它会修复它

android:configChanges="orientation|screenLayout|screenSize"