0
我知道這裏有關於使用方向和橫向佈局的問題,但是我想要做的有點不同。我的目標是在旋轉到橫向時完全更改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;
}
}
我不確定我完全理解。我是否在這兩個XML文件中包含一個片段?什麼進入片段標籤? –
請閱讀修改後的答案。並閱讀[this](http://developer.android.com/intl/es/training/basics/fragments/creating.html#AddInLayout).. – Msp
感謝您的快速回復。我得到的錯誤 - 錯誤膨脹類fragement。 我的兩個片段看起來像這 - <片段 的android:layout_width = 「match_parent」 機器人:layout_height = 「match_parent」 機器人:ID = 「@ + ID/frag2」 機器人:名字=「android.support。 v7.app.AppCompatDialogFragment「 /> –