2013-01-22 91 views
5

我需要移動屏幕左側的內容之外的版面內容,這樣我就可以使房間的滑動菜單右側 enter image description here如何移動屏幕的Android

下面是代碼:

// modify content layout params 
    try { 
     content = ((LinearLayout) act.findViewById(android.R.id.content) 
       .getParent()); 
    } catch (ClassCastException e) { 
     /* 
     * When there is no title bar 
     * (android:theme="@android:style/Theme.NoTitleBar"), the 
     * android.R.id.content FrameLayout is directly attached to the 
     * DecorView, without the intermediate LinearLayout that holds the 
     * titlebar plus content. 
     */ 
     content = (FrameLayout) act.findViewById(android.R.id.content); 
    } 
FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content 
        .getLayoutParams(); 
      pr.rightMargin = menuSize; 
      content.setLayoutParams(pr); 
// add the slide menu to parent 
    parent = (FrameLayout) content.getParent(); 

    try { 
     parent = (FrameLayout) content.getParent(); 
    } catch (ClassCastException e) { 
     /* 
     * Most probably a LinearLayout, at least on Galaxy S3. 
     */ 
     LinearLayout realParent = (LinearLayout) content.getParent(); 
     parent = new FrameLayout(act); 
     realParent.addView(parent, 0); // add FrameLayout to real parent of 
             // content 
     realParent.removeView(content); // remove content from real parent 
     parent.addView(content); // add content to FrameLayout 
    } 

    LayoutInflater inflater = (LayoutInflater) act 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    menu = inflater.inflate(R.layout.slidemenu, null); 

    FrameLayout.LayoutParams lays = new FrameLayout.LayoutParams(menuSize, 
      FrameLayout.LayoutParams.MATCH_PARENT, Gravity.RIGHT); 
    lays.setMargins(0, statusHeight, 0, 0); 
    menu.setLayoutParams(lays); 
    parent.addView(menu); 

但我得到的是: enter image description here 內容只是調整大小以適應屏幕,我怎樣才能使這項工作? 順便說一句我不能修改內容佈局它的相對佈局與surfaceview,但它應該與任何佈局,因爲我修改DecorView這是頂視圖容器

回答

3

要移動「屏幕」,您需要在視圖上調用getLayoutParams(),根據需要對其進行修改,然後在視圖上調用setLayoutParams()。

但對於一個偉大的教程如何實現菜單幻燈片在這裏看到: -

http://android.cyrilmottier.com/?p=658

要添加更多的幫助,下面是實現佈局就是我想你後: -

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/blue" > 


</LinearLayout> 

<LinearLayout 
    android:layout_width="200dip" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="-100dip" 
    android:background="@color/grey_divider" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 

這將導致出現50%的折扣屏幕的左手側的第二的LinearLayout。僅供參考您正在移動的LinearLayout將需要絕對寬度。但是您可以在xml中將其定義爲FILL_PARENT,然後獲取寬度,並在第一次將邊距設置爲負值時將其設置爲代碼中的絕對值。

希望這會有所幫助。

+0

我這樣做FrameLayout.LayoutParams pr =(android.widget.FrameLayout.LayoutParams)content .getLayoutParams(); pr.rightMargin = menuSize; content.setLayoutParams(pr);和菜單和動畫和所有準備就緒,我只需要這個修復 – user924941

+0

@ user924941我更新了我的答案昨天舉了一個例子,這有助於回答你的問題嗎? – Ryan

1

使用家長作爲班輪佈局,爲左和右正確的佈局視圖。 將您的左視圖放在horizo​​ntalScrollview中,需要將其滾動出屏幕。