问题
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;
}
}
回答1:
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.
来源:https://stackoverflow.com/questions/33976114/how-can-i-change-the-ui-when-in-portrait-and-landscape