2017-04-04 86 views
0

我差不多完成了一個應用程序,在該應用程序中,我使用底部導航欄在不同的佈局(不包括活動,如果相關)之間切換,以及一些自定義佈局適用於手機和平板電腦的橫向方向。當我在模擬器中運行應用程序時,我的應用程序的行爲與預期相同,但當我導出一個apk在我的設備(本例中爲手機)上測試時,該應用程序根本不會切換到橫向模式。在仿真器中按照預期方向工作,但不在設備上

讓我知道你是否希望我粘貼我認爲相關的部分代碼 - 我不知道從哪裏開始解決這個問題。

編輯:這裏是清單和活動文件:

清單:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".StoryListActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".StoryDetailActivity" 
     android:label="@string/title_story_detail" 
     android:parentActivityName=".StoryListActivity" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.compy.someappname.StoryListActivity" /> 
    </activity> 
</application> 

活動:

公共類StoryListAct ivity擴展AppCompatActivity {

private FrameLayout mainFrameLayout; 
private FrameLayout shopFrameLayout; 
private FrameLayout infoFrameLayout; 

/** 
* Whether or not the activity is in two-pane mode, i.e. running on a tablet 
* device. 
*/ 

private boolean mTwoPane; 

private int visiblePart; 

protected void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    if (mainFrameLayout.getVisibility() == View.VISIBLE) { 
     savedInstanceState.putInt("visiblePart", 0); 
    } else if (shopFrameLayout.getVisibility() == View.VISIBLE) { 
     savedInstanceState.putInt("visiblePart", 1); 
    } else if (infoFrameLayout.getVisibility() == View.VISIBLE) { 
     savedInstanceState.putInt("visiblePart", 2); 
    } 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (savedInstanceState != null){ 
     visiblePart = savedInstanceState.getInt("visiblePart"); 
    } 

    setContentView(R.layout.activity_story_list); 

    mainFrameLayout = (FrameLayout) findViewById(R.id.mainFrameLayout); 
    shopFrameLayout = (FrameLayout) findViewById(R.id.shopFrameLayout); 
    infoFrameLayout = (FrameLayout) findViewById(R.id.infoFrameLayout); 
    appBarLayout = (AppBarLayout) findViewById(R.id.app_bar); 
    TextView titleTextView = (TextView) findViewById(R.id.titleTextView); 
    TextView titleTextView2 = (TextView) findViewById(R.id.titleTextView2); 
    Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Ovo-Regular.ttf"); 
    titleTextView.setTypeface(typeface); 
    titleTextView2.setTypeface(typeface); 

    if (visiblePart == 0) { 
     mainFrameLayout.setVisibility(View.VISIBLE); 
     shopFrameLayout.setVisibility(View.GONE); 
     infoFrameLayout.setVisibility(View.GONE); 
    } else if (visiblePart == 1) { 
     mainFrameLayout.setVisibility(View.GONE); 
     shopFrameLayout.setVisibility(View.VISIBLE); 
     infoFrameLayout.setVisibility(View.GONE); 
    } else if (visiblePart == 2) { 
     mainFrameLayout.setVisibility(View.GONE); 
     shopFrameLayout.setVisibility(View.GONE); 
     infoFrameLayout.setVisibility(View.VISIBLE); 
    } 


    BottomNavigationView bottomNavigationView = (BottomNavigationView) 
      findViewById(R.id.bottom_navigation); 

    bottomNavigationView.setOnNavigationItemSelectedListener(
      new BottomNavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
        switch (item.getItemId()) { 
         case R.id.action_main: 
          mainFrameLayout.setVisibility(VISIBLE); 
          shopFrameLayout.setVisibility(GONE); 
          infoFrameLayout.setVisibility(GONE); 
          break; 
         case R.id.action_shop: 
          mainFrameLayout.setVisibility(GONE); 
          shopFrameLayout.setVisibility(VISIBLE); 
          infoFrameLayout.setVisibility(GONE); 
          break; 
         case R.id.action_info: 
          mainFrameLayout.setVisibility(GONE); 
          shopFrameLayout.setVisibility(GONE); 
          infoFrameLayout.setVisibility(VISIBLE); 
          break; 
        } 
        return false; 
       } 
      }); 

    View recyclerView = findViewById(R.id.story_list); 
    assert recyclerView != null; 
    setupRecyclerView((RecyclerView) recyclerView); 

    if (findViewById(R.id.story_detail_container) != null) { 
     // The detail container view will be present only in the 
     // large-screen layouts (res/values-w900dp). 
     // If this view is present, then the 
     // activity should be in two-pane mode. 
     mTwoPane = true; 
    } 
} 



private void setupRecyclerView(@NonNull RecyclerView recyclerView) { 
    recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS)); 
} 

public class SimpleItemRecyclerViewAdapter 
     extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> { 

    private final List<DummyContent.DummyItem> mValues; 

    public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) { 
     mValues = items; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.story_list_content, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder holder, int position) { 
     holder.mItem = mValues.get(position); 
     holder.mIdView.setText(mValues.get(position).id); 
     holder.mContentView.setText(mValues.get(position).story_title); 


     holder.mView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mTwoPane) { 
        Bundle arguments = new Bundle(); 
        arguments.putString(StoryDetailFragment.ARG_ITEM_ID, holder.mItem.id); 
        StoryDetailFragment fragment = new StoryDetailFragment(); 
        fragment.setArguments(arguments); 
        getSupportFragmentManager().beginTransaction() 
          .replace(R.id.story_detail_container, fragment) 
          .commit(); 
       } else { 
        Context context = v.getContext(); 
        Intent intent = new Intent(context, StoryDetailActivity.class); 
        intent.putExtra(StoryDetailFragment.ARG_ITEM_ID, holder.mItem.id); 

        context.startActivity(intent); 
       } 
      } 
     }); 
    } 

    @Override 
    public int getItemCount() { 
     return mValues.size(); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 
     public final View mView; 
     public final TextView mIdView; 
     public final TextView mContentView; 
     public DummyContent.DummyItem mItem; 

     public ViewHolder(View view) { 
      super(view); 
      mView = view; 
      mIdView = (TextView) view.findViewById(R.id.id); 
      mContentView = (TextView) view.findViewById(R.id.content); 
      Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Ovo-Regular.ttf"); 
      mIdView.setTypeface(typeface); 
      mContentView.setTypeface(typeface); 
     } 

     @Override 
     public String toString() { 
      return super.toString() + " '" + mContentView.getText() + "'"; 
     } 
    } 
} 

}

+0

安置自己的活動代碼和你的AndroidManifest.xml –

+0

傻之一:在您的手機的方向縱向鎖定? – BhalchandraSW

+0

愚蠢但合法 - 但它是在自動旋轉 – wxzwxe

回答

0

你可以試試這個

/** 
    * Getting screen orientation and ON rotation if it is OFF 
    */ 
    private void rotationON() { 
     if (Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 0) { 
      Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1); 
     } 
    } 

    /** 
    * Getting screen orientation and Off rotation if it is ON 
    */ 
    private void rotationOFF() { 
     if (Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1) { 
      Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0); 
     } 
    } 
+0

沒有打破我的應用程序在模擬器中,但遺憾的是沒有任何改變的設備行爲 – wxzwxe

+0

在設備設置,是它自動旋轉或鎖定? – DkThakur

+0

記得檢查 - 自動旋轉 – wxzwxe

相關問題