2016-12-29 148 views
2

我想設計佈局是這樣的:如何使用方向更改控制視圖可見性(可見/不可見)?

人像模式的xml:

<RelativeLayout> 
    <ToolBar> 
    <ToolBar> 
    <TextView> 
    </TextView> 
    <VideoView> 
    <VideoView> 
    <Webview> 
    </Webview> 
</RelativeLayout> 

風景模式的xml:

<RelativeLayout> 
    <VideoView> 
    <VideoView> 
</RelativeLayout> 

其他意見將是橫向模式不可見的。提前致謝。我是android開發的新手。

回答

1

由於你的佈局包含videoview我認爲你wnt在這個頁面上播放視頻。

爲了防止活動娛樂和視頻重新啓動,你應該在onConfigurationChanged

添加android:configChanges="orientation|screenSize"的活動宣言,​​比管理佈局只要找到onCreate`` than show hide them in onConfigurationChanged```

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // setContentView and another onCreate logic code 

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

    textview = findViewById(R.id.textview); 
    webView = findViewById(R.id.webview); 
    videoView = findViewById(R.id.videoview); 

    updateLayout(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    updateLayout(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE); 
} 

private void updateLayout(boolean isLandscape) { 
    if (isLandscape) { 
     textview.setVisibility(View.GONE); 
     webview.setVisibility(View.GONE); 
     getSupportActionBar().hide(); 
    } else { 
     textview.setVisibility(View.VISIBLE); 
     webview.setVisibility(View.VISIBLE); 
     getSupportActionBar().show(); 
    } 
} 
+0

謝謝。它工作完美。你最多可以保存2天。@Pogonets Antom –

0

視圖您可以創建兩個不同的佈局xml文件爲相同名稱的兩個不同的方向,並存儲在layout目錄中的縱向模式的文件和在layout-land目錄下的res目錄。 我認爲最好這樣做比管理運行時可見性更好

+0

這將中斷視頻播放 –

+0

@PogonetsAnton對不起,我沒有使用'VideoView'玩。如果它突破,那麼最好用你的答案。 –

+0

對不起。我的videoview與@Pogonets Antom完美結合。 –