2013-06-28 68 views
1

我在父線性佈局內部有兩個線性佈局。全部水平。我想全部顯示l_child並顯示部分r_child;而r_child的其餘部分將在屏幕右側顯示。我如何實現這一目標?android - 隱藏部分線性佈局在屏幕外向右

<LinearLayout 

    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/l_child" 
     android:orientation="horizontal" > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/r_child" 
     android:orientation="horizontal" > 
    </LinearLayout> 

</LinearLayout> 
+0

你可以發佈你想要做什麼的原理圖嗎? – Emmanuel

+0

不知道我理解,但在這裏:如果你願意,每個孩子的圖像線性佈局有三個圖像瀏覽;那麼我想要顯示左側孩子的所有三張圖像,但只顯示右側孩子的第一張圖片;而其餘兩個隱藏在屏幕右側。 –

+0

by schematic我的意思是顯示你想完成什麼的一個小圖畫 – Emmanuel

回答

0

佈局權重僅用於分佈佈局以填充視圖,因此不能通過這種方式進行溢出。

使用與密度無關的像素分配layout_width將導致不同大小設備的成功不同。

所以我相信你會有更多的成功添加你的佈局基於屏幕尺寸的編程。

查看此信息用於獲取屏幕寬度: Get screen dimensions in pixels

一旦你的屏幕寬度,則可以通過像素分配的內佈局尺寸(與LinearLayout.LayoutParams),如屏幕寬度的百分比來計算。

如果您希望左側佈局佔用屏幕的80%,請使用.8 * screen_width作爲大小,如果您希望右側佈局然後溢出20%的屏幕,請使用.4 * screen_width這個尺寸。

0

你的意思是這樣的

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

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_weight="1" 
android:orientation="horizontal" 
> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="70dp" 
    android:layout_gravity="center_horizontal|top"> 

    <ImageView 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:id="@+id/imageView1" 
     android:src="@drawable/ic_launcher" 
     /> 
    <ImageView 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:id="@+id/imageView2" 
     android:src="@drawable/ic_launcher"/> 
    <ImageView 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:id="@+id/imageView3" 
     android:src="@drawable/ic_launcher"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="70dp" 
    android:layout_gravity="center_horizontal|top"> 


    <ImageView 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:id="@+id/imageView1" 
     android:layout_marginRight="200dp" 
     android:src="@drawable/ic_launcher"/> 
    <ImageView 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:id="@+id/imageView2" 
     android:src="@drawable/ic_launcher"/> 
    <ImageView 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:id="@+id/imageView3" 
     android:src="@drawable/ic_launcher"/> 
</LinearLayout> 
</LinearLayout> 
4

先給家長LinearLayout負右邊緣。

android:layout_marginRight="-50dp" 

其他解決辦法可能是把父LinearLayout一個ScrollView內,並設置兒童佈局的寬度以所需的寬度。在這種情況下,您可以將右側佈局滾動回屏幕。

+0

我不認爲負面的尺寸在android中工作。 – eski

+0

AFAIK他們這樣做。 http://stackoverflow.com/questions/10673503/is-it-a-bad-practice-to-use-negative-margins-in-android我會很快檢查它以防萬一。 –

+0

有趣的是,我以爲我記得以前試過它沒有成功 – eski