1

我目前正在爲我的機器人小組開發一款應用程序,以用於即將到來的比賽。我一直在使用導航抽屜模板,我一直在努力工作,但遇到了一個主要問題。我發現每次我面向應用程序時,它都會從第一個片段轉到原始佈局。我通過android:configChanges="keyboardHidden|orientation|screenSize"將其修復爲AndroidManifest.xml,它修復了我原來的問題,但創建了另一個。雖然改變方向不會改變佈局或破壞片段,但應用程序不會加載我的橫向佈局,並且與layout-land文件夾中的文件夾相比,我的所有文本框都將不在位。我知道我必須使用onConfigurationChanged(Configuration newConfig),但我不明白我應該放在哪裏或者應該放在哪裏(在Java編程中不是很高級)。它應該在一個還是全部我的片段文件夾中?它應該在主要活動課上嗎?如果你能幫助我,那會很好。這裏是我的MainActivity.class,如果你需要我的片段類,請讓我知道。Android - 改變方向時未使用風景佈局

感謝,薩默爾

package com.compscitutorials.basigarcia.ramfernoscout; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

    NavigationView navigationView = null; 
    Toolbar toolbar = null; 

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

     //Set the fragment initially 
     WelcomeFragment fragment = new WelcomeFragment(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     navigationView = (NavigationView) findViewById(R.id.nav_view); 

     //How to change elements in the header programatically 
     View headerView = navigationView.getHeaderView(0); 
     TextView emailText = (TextView) headerView.findViewById(R.id.description); 
     emailText.setText("Scouting Application"); 

     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.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); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_welcome) { 
      //Set the fragment initially 
      WelcomeFragment fragment = new WelcomeFragment(); 
      android.support.v4.app.FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.replace(R.id.fragment_container, fragment); 
      fragmentTransaction.commit(); 
      // Handle the camera action 
     } else if (id == R.id.nav_facebook) { 
      FacebookFragment fragment = new FacebookFragment(); 
      android.support.v4.app.FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.replace(R.id.fragment_container, fragment); 
      fragmentTransaction.commit(); 
     } else if (id == R.id.nav_members) { 
      //Set the fragment initially 
      MembersFragment fragment = new MembersFragment(); 
      android.support.v4.app.FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.replace(R.id.fragment_container, fragment); 
      fragmentTransaction.commit(); 
     } else if (id == R.id.nav_robot) { 

     } else if (id == R.id.nav_scout) { 

     } else if (id == R.id.nav_match) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } //End of onNavigationItemSelected 
} //End of class 
+1

先前即使用不指定機器人的方法:在清單configChanges是正確的,你需要存儲的活動和碎片狀態,然後當活動重新恢復它,作爲定向活動被破壞並重新創建。查看此[post](http://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack)以獲得更多理解 –

回答

0

在活動中加入此功能。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } 
} 
+0

試過這個,但不幸的是沒有工作 –