2015-07-20 46 views
0

我已經創建了一個球形視頻播放器,並希望防止它包含在視圖中的旋轉(包含3D場景的Unity視圖的Framelayout。) 我想要控件旋轉(它們存在於容器內的相對佈局中)。 但我不想統一容器旋轉。這可能嗎?如何不旋轉Android佈局的特定元素

這裏的佈局(所有控件都在佈局):

<FrameLayout 
      android:id="@+id/unity_layout" 
      android:visibility="invisible" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

     <FrameLayout 
       android:id="@+id/unity_container" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
     </FrameLayout> 

     <include layout="@layout/player_controls"/> 

</FrameLayout> 

圖片:https://dl.dropboxusercontent.com/u/17087393/pivotalscreens/Screenshot_2015-07-19-23-36-31.jpg

+0

是y你試圖阻止整個屏幕旋轉? http://stackoverflow.com/questions/2730855/prevent-screen-rotation-android – JDenais

+0

圖像鏈接不可訪問。 – Gagan

回答

0

我可以建議你去下面的解決方案,

步驟1,創建兩種不同的佈局(layout & layout_land)並定義設計

St EP 2.使用擴展FrameLayout()

第3步:您的自定義視圖取代常規的FrameLayout自定義視圖,MyNonChangingLayout()的

<MyNonChangingLayout 
android:id="@+id/unity_container" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
</MyNonChangingLayout> 

例,

MyNonChangingLayout extends FrameLayout { 
     MyNonchangingLayout(Context content) { 
      super(context); 
      myContext = context; 
      makeFromXML(); 
     } 

    private void makeFromXML() { 
     LayoutInflater inflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     topView = inflater.inflate(MyR.layout.my_non_changing_layout, this, false); 
     super.addView(topView); 
    } 

    /* 
    * Then, you can override quite a lot of the layout's calls and 
    * enforce behaviour on the children. Two examples: 
    */ 

    // To specifically catch orientation changes 
    @Overridge 
    onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     // You could create the layout here by removing all views and rebuilding them 
     // Perhaps by having a two xml layouts, one of which is "90 degrees out" ... 
     // If you do make the layot here, make sure you don't clash with the constructor code! 
     switch (newConfig.orientation) { 
      case ORIENTATION_LANDSCAPE: 
       // Make the layout for this orientation (as per above) 
       break; 
      case ORIENTATION_PORTRAIT: 
       // Make the layout for this orientation (as per above) 
       break; 
      case ORIENTATION_SQUARE: 
       // Make the layout for this orientation (as per above) 
       break; 
     } 
    } 

    //to handle size changes to enforce aspect ratios on children: 
    @override 
    protected void onSizeChanged (int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 

     int viewWidth = //something I've determine 
     int viewHeight = //something I've determined 
     setViewsize(viewToHaveSizedControlled, viewWidth, viewheight); 
    } 

    // The post thing means that it doesn't crash no matter which thread it is 
    // executed on ... 
    private void setViewsize(final View v, final int w, final int h) { 
     post(new Runnable() { 
      public void run() { 
       ViewGroup.LayoutParams lp = v.getLayoutParams(); 
       lp.width = w; 
       lp.height = h; 
       v.setLayoutParams(lp); 
     }}); 
    } 

這個答案本來這裏給出,

How to prevent orientation change for just a layout, not the whole screen/activity