如何在纵向和横向中更改UI

时间:2015-11-28 20:27:32

标签: java android xml android-studio

我知道这里已经有关于使用方向和横向布局的问题,但我想要做的是有点不同。我的目标是在旋转到横向时完全改变UI,例如当肖像显示常规信息但旋转显示表格时。

这是否涉及两个单独的活动并使用if语句根据方向启动活动?我尝试过使用简单的横向布局,但是当我更改方向时,它会针对纵向布局中使用的项目的所有findviewbyids的nullpointerexception关闭。

为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的完整性

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;
    }
}

1 个答案:

答案 0 :(得分:2)

使用单个活动和两个片段。看看这个例子,

<强> MainActivity.java

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

    }
}

<强> RES /布局/ 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 /布局脊/ 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 /布局/ 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 /布局/ 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>

这将在纵向屏幕中显示&#39;纵向&#39; ,在横向屏幕中显示&#39;横向&#39;

希望它有所帮助。