我怎样才能改变纵向和横向当UI [英] How can i change the UI when in portrait and landscape

查看:176
本文介绍了我怎样才能改变纵向和横向当UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解这里已经有问题,使用方向和横向的布局,这只是我想要做的是有点不同。我的目标,当旋转为横向完全更改UI例如人像时,它显示常规信息,但旋转时,显示的表。

这会涉及到有两个独立的活动,并使用if语句根据方向开始活动?我一直在使用一个简单的横向布局尝试,但是当我改变方向它关闭对所有findviewbyids在纵向布局使用的物品一个NullPointerException异常。

加code为manusunny

 公共类PortraitFragment扩展FragmentActivity {
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){            查看查看= inflater.inflate(R.layout.content_main,集装箱,FALSE);
            ImageView的ImageView的=(ImageView的)view.findViewById(R.id.imageView);
            TextView的lessonString =(TextView中)view.findViewById(R.id.primaryText);
            TextView的subTextString =(TextView中)view.findViewById(R.id.secondaryText);
            返回视图。
        }
    }
公共类LandscapeFragment扩展FragmentActivity {    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){        查看查看= inflater.inflate(R.layout.content_portrait,集装箱,FALSE);
        ImageView的ImageView的=(ImageView的)view.findViewById(R.id.imageView);
        TextView的lessonString =(TextView中)view.findViewById(R.id.primaryText);
        TextView的subTextString =(TextView中)view.findViewById(R.id.secondaryText);
        返回视图。    }
}

在MainActivty其条款内容

 包com.example.harris.doitrightyoustupidcretin;进口android.content.Intent;
进口android.os.Bundle;
进口android.support.design.widget.FloatingActionButton;进口android.support.design.widget.Snackbar;
进口android.support.v7.app.AppCompatActivity;
进口android.support.v7.widget.Toolbar;
进口android.util.Log;
进口android.view.View;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.support.v4.app.FragmentActivity;
进口com.example.harris.doitrightyoustupidcretin.R;进口org.joda.time.LocalTime;进口的java.util.Calendar;公共类MainActivity扩展AppCompatActivity {    私有静态最后弦乐TAG =MyLog;
    字符串的HistoryInfo [] =新的String [2];
    字符串computingInfo [] =新的String [2];
    字符串reInfo [] =新的String [2];
    字符串biologyInfo [] =新的String [2];
    字符串physicsInfo [] =新的String [2];
    字符串chemistryInfo [] =新的String [2];
    字符串frenchInfo [] =新的String [2];
    字符串englishInfo [] =新的String [2];
    字符串mathsInfo [] =新的String [2];
    字符串gamesInfo [] =新的String [2];    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        工具条工具栏=(栏)findViewById(R.id.toolbar);
        setSupportActionBar(工具栏);
        的HistoryInfo [0] =H1;
        的HistoryInfo [1] =布拉德伯里小姐;        mathsInfo [0] =MA1;
        mathsInfo [1] =劳伦斯小姐;        biologyInfo [0] =BL1;
        biologyInfo [1] =吉布森先生;        chemistryInfo [0] =CL2;
        chemistryInfo [1] =博士鲍尔斯;        frenchInfo [0] =LA8;
        frenchInfo [1] =贝利小姐;        reInfo [0] =RE2;
        reInfo [1] =迪莫克先生;        englishInfo [0] =ER1;
        englishInfo [1] =理查兹小姐;        gamesInfo [0] =运动场
        gamesInfo [1] =普格先生;        computingInfo [0] =IT1;
        computingInfo [1] =格林先生        physicsInfo [0] =PL2;
        physicsInfo [1] =埃文斯先生;
        字符串nextLessonString =默认;        nextlesson();        setLessonText();        FloatingActionButton FAB =(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                nextlesson();
            }
        });
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }
    公共无效setLessonText(){
        ImageView的ImageView的=(ImageView的)findViewById(R.id.imageView);
        TextView的lessonString =(的TextView)findViewById(R.id.primaryText);
        TextView的subTextString =(的TextView)findViewById(R.id.secondaryText);        lessonString.setText(nextlesson());        如果(nextlesson()==历史){
            imageView.setImageResource(R.drawable.history);
            subTextString.setText(的HistoryInfo [1] + - +的HistoryInfo [0]);
        }
        如果(nextlesson()==数学){
            imageView.setImageResource(R.drawable.maths);
            subTextString.setText(mathsInfo [1] + - + mathsInfo [0]);
        }
        如果(nextlesson()==英语){
            imageView.setImageResource(R.drawable.english);
            subTextString.setText(englishInfo [1] + - + englishInfo [0]);
        }
        如果(nextlesson()==法国){
            imageView.setImageResource(R.drawable.french);
            subTextString.setText(frenchInfo [1] + - + frenchInfo [0]);
        }
        如果(nextlesson()==计算){
            imageView.setImageResource(R.drawable.computing);
            subTextString.setText(computingInfo [1] + - + computingInfo [0]);
        }
        如果(nextlesson()==生物学){
            imageView.setImageResource(R.drawable.biology);
            subTextString.setText(biologyInfo [1] + - + biologyInfo [0]);
        }
        如果(nextlesson()==化学){
            imageView.setImageResource(R.drawable.chemistry);
            subTextString.setText(chemistryInfo [1] + - + chemistryInfo [0]);
        }
        如果(nextlesson()==物理){
            imageView.setImageResource(R.drawable.physics);
            subTextString.setText(physicsInfo [1] + - + physicsInfo [0]);
        }
        如果(nextlesson()==RE){
            imageView.setImageResource(R.drawable.re);
            subTextString.setText(reInfo [1] + - + reInfo [0]);
        }
    }
    公共字符串nextlesson(){
        日历C = Calendar.getInstance();
        INT weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
        本地时间currentTime的= LocalTime.now();
        INT nextLessonNumber = 0;
        串nextLessonString;
        INT currentLesson = 0;        本地时间lesson1Start =新的本地时间(09:15);
        本地时间lesson1End =新的本地时间(10:15);
        本地时间lesson2St​​art =新的本地时间(10:15);
        本地时间lesson3Start =新的本地时间(11:40);
        本地时间lesson4start =新的本地时间(13:45);
        本地时间lesson5Start =新的本地时间(14:45);
        本地时间endOfDay =新的本地时间(15:50);        INT一周中的某天= c.get(Calendar.DAY_OF_WEEK);
        INT dateDays = c.get(Calendar.DAY_OF_MONTH);
        INT dateMonth = c.get(的Calendar.MONTH);        如果(currentTime.isBefore(lesson1Start)){
            // P1下一
            nextLessonNumber = 1;
            Log.d(TAG,它的工作);
            currentLesson = 0;
        }否则如果(currentTime.isAfter(lesson1Start)及&放大器; currentTime.isBefore(lesson1End)){
            //下一P2
            nextLessonNumber = 2;
            Log.d(TAG,它的工作);
            currentLesson = 1;
        }否则如果(currentTime.isAfter(lesson2St​​art)及&放大器; currentTime.isBefore(lesson3Start)){
            //接下来P3
            nextLessonNumber = 3;
            Log.d(TAG,它的工作);
            currentLesson = 2;
        }否则如果(currentTime.isAfter(lesson3Start)及&放大器; currentTime.isBefore(lesson4start)){
            //接下来的P4
            nextLessonNumber = 4;
            Log.d(TAG,它的工作);
            currentLesson = 3;
        }否则如果(currentTime.isAfter(lesson4start)及&放大器; currentTime.isBefore(lesson5Start)){
            //接下来P5
            nextLessonNumber = 5;
            Log.d(TAG,它的工作);
            currentLesson = 4;
        }否则如果(currentTime.isAfter(lesson5Start)及&放大器; currentTime.isAfter(endOfDay)){
            currentLesson = 5;
        }其他{
            nextLessonNumber = 0;
            Log.d(TAG,它没有工作);
        }        如果((weekOfMonth == 1)||(weekOfMonth == 3)){
            Log.d(TAG,这让这么远);            开关(星期几){
                案例1://周日无能为力                    返回星期天;
                案例2://星期一周乙
                    如果(nextLessonNumber == 1){
                        回归数学;                    }否则如果(nextLessonNumber == 2){
                        返回物理;
                    }否则如果(nextLessonNumber == 3){
                        回到历史;
                    }否则如果(nextLessonNumber == 4){
                        返回化学;
                    }否则如果(nextLessonNumber == 5){
                        返回法国;
                    }其他{                    }
                    打破;                案例3://周二周乙
                    Log.d(TAG,哟它的工作哟);
                    如果(nextLessonNumber == 1){
                        返回RE;                    }否则如果(nextLessonNumber == 2){
                        返回英语;
                    }否则如果(nextLessonNumber == 3){
                        回归数学;
                    }否则如果(nextLessonNumber == 4){
                        返回计算;
                    }否则如果(nextLessonNumber == 5){
                        回到生物学
                    }其他{                    }
                    打破;                壳4://星期三周乙
                    如果(currentLesson == 1){
                        返回英语;                    }否则如果(nextLessonNumber == 2){
                        返回计算;
                    }否则如果(nextLessonNumber == 3){
                        返回化学;
                    }否则如果(nextLessonNumber == 4){
                        返回法国;
                    }否则如果(nextLessonNumber == 5){
                        回到历史;
                    }其他{                    }
                    打破;                案例5://周四周乙
                    Log.d(TAGThursWeekB);
                    如果(nextLessonNumber == 1){
                        返回法国;                    }否则如果(nextLessonNumber == 2){
                        回到历史;
                    }否则如果(nextLessonNumber == 3){
                        返回物理;
                    }否则如果(nextLessonNumber == 4){
                        返回游戏;
                    }否则如果(nextLessonNumber == 5){
                        返回游戏;
                    }其他{                    }
                    打破;                案例6://周五周乙
                    如果(nextLessonNumber == 1){
                        返回RE;                    }否则如果(nextLessonNumber == 2){
                        回到生物学
                    }否则如果(nextLessonNumber == 3){
                        返回计算;
                    }否则如果(nextLessonNumber == 4){
                        返回英语;
                    }否则如果(nextLessonNumber == 5){
                        回归数学;
                    }其他{                    }
                    打破;
                案例7://周六无能为力
                        返回星期六;            }        }其他{            开关(星期几){
                案例1://周日无能为力                    返回星期天;
                案例2://星期一周
                    如果(nextLessonNumber == 1){
                        回归数学;                    }否则如果(nextLessonNumber == 2){
                        返回英语;                    }否则如果(nextLessonNumber == 3){
                        返回计算;                    }否则如果(nextLessonNumber == 4){
                        返回RE;
                    }否则如果(nextLessonNumber == 5){
                        返回物理;
                    }其他{                    }
                    打破;                案例3://周二周
                    Log.d(TAG,它的工作哟pNOTHING);
                    如果(nextLessonNumber == 1){
                        回归数学;                    }否则如果(nextLessonNumber == 2){
                        返回法国;
                    }否则如果(nextLessonNumber == 3){
                        回到历史;
                    }否则如果(nextLessonNumber == 4){
                        返回英语;
                    }否则如果(nextLessonNumber == 5){
                        返回化学;
                    }其他{                    }
                    打破;                案例4://周三周
                    如果(nextLessonNumber == 1){
                        返回英语;
                    }否则如果(nextLessonNumber == 2){
                        返回计算;
                    }否则如果(nextLessonNumber == 3){
                        回到生物学
                    }否则如果(nextLessonNumber == 4){
                        回到历史;
                    }否则如果(nextLessonNumber == 5){
                        返回法国;
                    }其他{                    }
                    打破;                案例5://周四一周
                    Log.d(TAG,它的工作哟周四);
                    如果(nextLessonNumber == 1){
                        返回化学;                    }否则如果(nextLessonNumber == 2){
                        返回法国;
                    }否则如果(nextLessonNumber == 3){
                        回归数学;
                    }否则如果(nextLessonNumber == 4){
                        返回游戏;
                    }否则如果(nextLessonNumber == 5){
                        返回游戏;
                    }其他{                    }
                    打破;
                案例6://周五周
                    如果(nextLessonNumber == 1){
                        回到历史;                    }否则如果(nextLessonNumber == 2){
                        返回物理;
                    }否则如果(nextLessonNumber == 3){
                        返回计算;
                    }否则如果(nextLessonNumber == 4){
                        回归数学;
                    }否则如果(nextLessonNumber == 5){
                        回到生物学
                    }其他{                    }
                    打破;
                案例7://周六无能为力
                    返回星期六;            }
        }返回null;
    }
}


解决方案

使用一个单一的活动,两个片段。看看下面这个例子,

MainActivity.java

 公共类MainActivity扩展AppCompatActivity {
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);    }
}

RES /布局/ activity_main.xml中

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=com.manusunny.myapplication.MainActivity
    机器人:方向=垂直>    <片段机器人:名字=com.manusunny.myapplication.PortraitFragment
        机器人:ID =@ + ID / portrait_fragment
        机器人:layout_width =match_parent
        机器人:layout_height =30dp/>
< / LinearLayout中>

RES /布局土地/ activity_main.xml中

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=com.manusunny.myapplication.MainActivity
    机器人:方向=垂直>    <片段机器人:名字=com.manusunny.myapplication.LandscapeFragment
        机器人:ID =@ + ID / landscape_fragment
        机器人:layout_width =match_parent
        机器人:layout_height =30dp/>
< / LinearLayout中>

PortraitFragment.java

 公共类PortraitFragment扩展片段{
    公共PortraitFragment(){
        //必需的空公共构造
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                         捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.fragment_portrait,集装箱,FALSE);
        // 做一点事
        返回查看
    }
}

RES /布局/ fragment_portrait.xml

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=com.manusunny.myapplication.PortraitFragment>    <的TextView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:文字=纵向/>
< /&的FrameLayout GT;

LandscapeFragment.java

 公共类LandscapeFragment扩展片段{
    公共LandscapeFragment(){
        //必需的空公共构造
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                         捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.fragment_landscape,集装箱,FALSE);
        // 做一点事
        返回查看
    }
}

RES /布局/ fragment_landscape.xml

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=com.manusunny.myapplication.LandscapeFragment>    <的TextView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:文字=风景/>
< /&的FrameLayout GT;

这将显示的肖像的纵向屏幕的山水的横向屏幕。

希望它帮助。

I understand there are already questions here on using orientation and landscape layouts however what I want to do is a bit different. I am aiming to change the UI entirely when rotated to landscape e.g when portrait it displays regular information but when rotated shows a table.

Would this involve having two separate activities and using an if statement to start the activity depending on orientation? I have tried using a simple landscape layout but when I change the orientation it closes on a nullpointerexception for all the findviewbyids for items used in the portrait layout.

Added code for manusunny

    public class PortraitFragment extends FragmentActivity {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view =  inflater.inflate(R.layout.content_main, container, false);
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            TextView lessonString = (TextView) view.findViewById(R.id.primaryText);
            TextView subTextString = (TextView) view.findViewById(R.id.secondaryText);
            return view;
        }
    }


public class LandscapeFragment extends FragmentActivity {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.content_portrait, container, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
        TextView lessonString = (TextView) view.findViewById(R.id.primaryText);
        TextView subTextString = (TextView) view.findViewById(R.id.secondaryText);
        return view;

    }
}

MainActivty in its entirity

package com.example.harris.doitrightyoustupidcretin;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.FragmentActivity;
import com.example.harris.doitrightyoustupidcretin.R;

import org.joda.time.LocalTime;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MyLog";


    String historyInfo[] = new String[2];
    String computingInfo[] = new String[2];
    String reInfo[] = new String[2];
    String biologyInfo[] = new String[2];
    String physicsInfo[] = new String[2];
    String chemistryInfo[] = new String[2];
    String frenchInfo[] = new String[2];
    String englishInfo[] = new String[2];
    String mathsInfo[] = new String[2];
    String gamesInfo[] = new String[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        historyInfo[0] = "H1";
        historyInfo[1] = "Miss Bradbury";

        mathsInfo[0] = "MA1";
        mathsInfo[1] = "Miss Lawrence";

        biologyInfo[0] = "BL1";
        biologyInfo[1] = "Mr Gibson";

        chemistryInfo[0] = "CL2";
        chemistryInfo[1] = "Dr Bowers";

        frenchInfo[0] = "LA8";
        frenchInfo[1] = "Miss Bailey";

        reInfo[0] = "RE2";
        reInfo[1] = "Mr Dimmock";

        englishInfo[0] = "ER1";
        englishInfo[1] = "Miss Richards";

        gamesInfo[0] = "Sports Hall";
        gamesInfo[1] = "Mr Pugh";

        computingInfo[0] = "IT1";
        computingInfo[1] = "Mr Green";

        physicsInfo[0] = "PL2";
        physicsInfo[1] = "Mr Evans";


        String nextLessonString = "default";



        nextlesson();

        setLessonText();



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextlesson();
            }
        });
    }

    @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) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void setLessonText(){
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        TextView lessonString = (TextView) findViewById(R.id.primaryText);
        TextView subTextString = (TextView) findViewById(R.id.secondaryText);

        lessonString.setText(nextlesson());

        if (nextlesson()== "History"){
            imageView.setImageResource(R.drawable.history);
            subTextString.setText(historyInfo[1] + " - "+historyInfo[0]);
        }
        if (nextlesson()== "Maths"){
            imageView.setImageResource(R.drawable.maths);
            subTextString.setText(mathsInfo[1] + " - " + mathsInfo[0]);
        }
        if (nextlesson()== "English"){
            imageView.setImageResource(R.drawable.english);
            subTextString.setText(englishInfo[1] + " - " + englishInfo[0]);
        }
        if (nextlesson()== "French"){
            imageView.setImageResource(R.drawable.french);
            subTextString.setText(frenchInfo[1] + " - " + frenchInfo[0]);
        }
        if (nextlesson()== "Computing"){
            imageView.setImageResource(R.drawable.computing);
            subTextString.setText(computingInfo[1] + " - " + computingInfo[0]);
        }
        if (nextlesson()== "Biology"){
            imageView.setImageResource(R.drawable.biology);
            subTextString.setText(biologyInfo[1] + " - " + biologyInfo[0]);
        }
        if (nextlesson()== "Chemistry"){
            imageView.setImageResource(R.drawable.chemistry);
            subTextString.setText(chemistryInfo[1] + " - " + chemistryInfo[0]);
        }
        if (nextlesson()== "Physics"){
            imageView.setImageResource(R.drawable.physics);
            subTextString.setText(physicsInfo[1] + " - " + physicsInfo[0]);
        }
        if (nextlesson()== "RE"){
            imageView.setImageResource(R.drawable.re);
            subTextString.setText(reInfo[1] + " - " + reInfo[0]);
        }
    }


    public String nextlesson() {
        Calendar c = Calendar.getInstance();
        int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
        LocalTime currentTime = LocalTime.now();
        int nextLessonNumber = 0;
        String nextLessonString;
        int currentLesson = 0;

        LocalTime lesson1Start = new LocalTime("09:15");
        LocalTime lesson1End = new LocalTime("10:15");
        LocalTime lesson2Start = new LocalTime("10:15");
        LocalTime lesson3Start = new LocalTime("11:40");
        LocalTime lesson4start = new LocalTime("13:45");
        LocalTime lesson5Start = new LocalTime("14:45");
        LocalTime endOfDay = new LocalTime ("15:50");

        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int dateDays = c.get(Calendar.DAY_OF_MONTH);
        int dateMonth = c.get(Calendar.MONTH);

        if (currentTime.isBefore(lesson1Start)) {
            //p1 next
            nextLessonNumber = 1;
            Log.d(TAG, "It worked");
            currentLesson = 0;
        } else if (currentTime.isAfter(lesson1Start) && currentTime.isBefore(lesson1End)) {
            //p2 next
            nextLessonNumber = 2;
            Log.d(TAG, "It worked");
            currentLesson = 1;
        } else if (currentTime.isAfter(lesson2Start) && currentTime.isBefore(lesson3Start)) {
            //p3 next
            nextLessonNumber = 3;
            Log.d(TAG, "It worked");
            currentLesson = 2;
        } else if (currentTime.isAfter(lesson3Start) && currentTime.isBefore(lesson4start)) {
            //p4 next
            nextLessonNumber = 4;
            Log.d(TAG, "It worked");
            currentLesson = 3;
        } else if (currentTime.isAfter(lesson4start) && currentTime.isBefore(lesson5Start)) {
            //p5 next
            nextLessonNumber = 5;
            Log.d(TAG, "It worked");
            currentLesson = 4;
        } else if (currentTime.isAfter(lesson5Start) && currentTime.isAfter(endOfDay)) {
            currentLesson = 5;
        } else {
            nextLessonNumber = 0;
            Log.d(TAG, "It didn't worked");
        }





        if ((weekOfMonth == 1) || (weekOfMonth == 3)) {
            Log.d(TAG, "It made it this far");

            switch (dayOfWeek) {
                case 1: //Sunday DO NOTHING

                    return "Sunday";
                case 2: //Monday WEEK B
                    if (nextLessonNumber == 1) {
                        return "Maths";

                    } else if (nextLessonNumber == 2) {
                        return "Physics";
                    } else if (nextLessonNumber == 3) {
                        return "History";
                    } else if (nextLessonNumber == 4) {
                        return "Chemistry";
                    } else if (nextLessonNumber == 5) {
                        return "French";
                    }else {

                    }
                    break;

                case 3: //Tuesday WEEK B
                    Log.d(TAG, "Yo it worked yo");
                    if (nextLessonNumber == 1) {
                        return "RE";

                    } else if (nextLessonNumber == 2) {
                        return "English";
                    } else if (nextLessonNumber == 3) {
                        return "Maths";
                    } else if (nextLessonNumber == 4) {
                        return "Computing";
                    } else if (nextLessonNumber == 5) {
                        return "Biology";
                    } else {

                    }
                    break;

                case 4: //Wednesday WEEK B
                    if (currentLesson == 1) {
                        return "English";

                    } else if (nextLessonNumber == 2) {
                        return "Computing";
                    } else if (nextLessonNumber == 3) {
                        return "Chemistry";
                    } else if (nextLessonNumber == 4) {
                        return "French";
                    } else if (nextLessonNumber == 5) {
                        return "History";
                    }else {

                    }
                    break;

                case 5: //Thursday WEEK B
                    Log.d(TAG, "ThursWeekB");
                    if (nextLessonNumber == 1) {
                        return "French";

                    } else if (nextLessonNumber == 2) {
                        return "History";
                    } else if (nextLessonNumber == 3) {
                        return "Physics";
                    } else if (nextLessonNumber == 4) {
                        return "Games";
                    } else if (nextLessonNumber == 5) {
                        return "Games";
                    }else {

                    }
                    break;

                case 6: //Friday WEEK B
                    if (nextLessonNumber == 1) {
                        return "RE";

                    } else if (nextLessonNumber == 2) {
                        return "Biology";
                    } else if (nextLessonNumber == 3) {
                        return "Computing";
                    } else if (nextLessonNumber == 4) {
                        return "English";
                    } else if (nextLessonNumber == 5) {
                        return "Maths";
                    }else {

                    }
                    break;
                case 7: //Saturday DO NOTHING
                        return "Saturday";

            }

        } else {

            switch (dayOfWeek) {
                case 1: //Sunday DO NOTHING

                    return "Sunday";
                case 2: //Monday WEEK A
                    if (nextLessonNumber == 1) {
                        return "Maths";

                    } else if (nextLessonNumber == 2) {
                        return "English";

                    } else if (nextLessonNumber == 3) {
                        return "Computing";

                    } else if (nextLessonNumber == 4) {
                        return "RE";
                    } else if (nextLessonNumber == 5) {
                        return "Physics";
                    }else {

                    }
                    break;

                case 3: //Tuesday WEEK A
                    Log.d(TAG, "It worked yo pNOTHING");
                    if (nextLessonNumber == 1) {
                        return "Maths";

                    } else if (nextLessonNumber == 2) {
                        return "French";
                    } else if (nextLessonNumber == 3) {
                        return "History";
                    } else if (nextLessonNumber == 4) {
                        return "English";
                    } else if (nextLessonNumber == 5) {
                        return "Chemistry";
                    }else {

                    }
                    break;

                case 4: //Wednesday WEEK A
                    if (nextLessonNumber == 1) {
                        return "English";
                    } else if (nextLessonNumber == 2) {
                        return "Computing";
                    } else if (nextLessonNumber == 3) {
                        return "Biology";
                    } else if (nextLessonNumber == 4) {
                        return "History";
                    } else if (nextLessonNumber == 5) {
                        return "French";
                    }else {

                    }
                    break;

                case 5: //Thursday WEEK A
                    Log.d(TAG, "It worked yo thurs");
                    if (nextLessonNumber == 1) {
                        return "Chemistry";

                    } else if (nextLessonNumber == 2) {
                        return "French";
                    } else if (nextLessonNumber == 3) {
                        return "Maths";
                    } else if (nextLessonNumber == 4) {
                        return "Games";
                    } else if (nextLessonNumber == 5) {
                        return "Games";
                    }else {

                    }
                    break;


                case 6: //Friday WEEK A
                    if (nextLessonNumber == 1) {
                        return "History";

                    } else if (nextLessonNumber == 2) {
                        return "Physics";
                    } else if (nextLessonNumber == 3) {
                        return "Computing";
                    } else if (nextLessonNumber == 4) {
                        return "Maths";
                    } else if (nextLessonNumber == 5) {
                        return "Biology";
                    }else {

                    }
                    break;
                case 7: //Saturday DO NOTHING
                    return "Saturday";

            }
        }

return null;
    }
}

解决方案

Use a single activity and two fragments. Look at this example,

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manusunny.myapplication.MainActivity"
    android:orientation="vertical">

    <fragment android:name="com.manusunny.myapplication.PortraitFragment"
        android:id="@+id/portrait_fragment"
        android:layout_width="match_parent"
        android:layout_height="30dp" />
</LinearLayout>

res/layout-land/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manusunny.myapplication.MainActivity"
    android:orientation="vertical">

    <fragment android:name="com.manusunny.myapplication.LandscapeFragment"
        android:id="@+id/landscape_fragment"
        android:layout_width="match_parent"
        android:layout_height="30dp" />
</LinearLayout>

PortraitFragment.java

public class PortraitFragment extends Fragment {
    public PortraitFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_portrait, container, false);
        // do something
        return view
    }
}

res/layout/fragment_portrait.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manusunny.myapplication.PortraitFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Portrait" />
</FrameLayout>

LandscapeFragment.java

public class LandscapeFragment extends Fragment {
    public LandscapeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_landscape, container, false);
        // do something
        return view
    }
}

res/layout/fragment_landscape.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manusunny.myapplication.LandscapeFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Landscape" />
</FrameLayout>

This will show 'Portrait' in portrait screen and 'Landscape' in landscape screen.

Hope it helps.

这篇关于我怎样才能改变纵向和横向当UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆