2013-03-19 95 views
0

我有一個小的側欄片段,我加上行動項目選擇。它有一個固定的寬度,我的願望是,主視圖將全屏顯示,直到行動選項被選中時,它會被稍微壓縮以允許邊欄出現。Android的片段側邊欄

我已經嘗試了很多東西,包括相對的toleftOf/RightOf和各種佈局/寬度組合。除非我使用具有權重的LinearLayout,否則我總是以側欄顯示全屏。半工作LinearLayout重量的問題在於它使用屏幕的百分比,這會在不同的屏幕尺寸上導致不可靠的效果。

有沒有人拉掉類似的東西?

______ _ 
|  | | | 
| Main |<-|S| 
|______| |_| 

目前半使用XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" > 

    <FrameLayout 
     android:id="@+id/frameLayoutImage" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 
    </FrameLayout> 

    <!-- This will initially have no content/collapsed --> 
    <FrameLayout 
     android:id="@+id/frameLayoutEdit" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="right|top" 
     android:layout_marginBottom="40dp" 
     android:layout_weight="4" > 

    </FrameLayout> 

</LinearLayout> 

片段XML(可以是固定的寬度):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout /> 

    <Button /> 

    <Button /> 

</RelativeLayout> 

片段此外:

// This should just push the frame layout the necessary width to display the fragment 
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
transaction.add(R.id.frameLayoutEdit, xmpFrag); 

回答

1

不要在側欄上設置重量;設置一個固定大小(或wrap_content)並將主內容的layout_width設置爲0dp。

根據您想要的效果,您可能希望將它們都放在FrameLayout中,以便邊欄覆蓋主要片段而不是縮小主要片段,使主要片段始終填充,並且側邊欄只是它自己的大小。

+0

由於到一些元素的方向我不想疊加。這工作了一些修改。 主要 1. layout_width:0dp 2. layout_weight:1個 邊欄: – Anthony 2013-03-19 04:15:30

+0

(被鎖整理註釋編輯出) 這個工作有一些修改。 主要的FrameLayout 1 layout_weight:1 注:layout_width:0dp需要的重量,但並不能改變什麼 側邊欄的FrameLayout: 1. layout_width:WRAP_CONTENT 2.沒有重量 邊欄: 1. layout_width:300dp 注意:300對你的內容是任意的,wrap_content使它永遠不會顯示 – Anthony 2013-03-19 04:29:55

+0

你的側邊欄是一個自定義視圖嗎?如果是這樣,你應該重寫onMeasure以允許wrap_content工作。 – 2013-03-19 04:40:09

0

這是實際工作的代碼上面的答案(感謝斯科特幫助我做出合乎邏輯的飛躍):

<FrameLayout 
    android:id="@+id/frameLayoutImage" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
</FrameLayout> 

<!-- This will initially have no content/collapsed --> 
<FrameLayout 
    android:id="@+id/frameLayoutEdit" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="right|top" 
    android:layout_marginBottom="40dp" > 

</FrameLayout> 

片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="300dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout /> 

    <Button /> 

    <Button /> 

</RelativeLayout>