2010-12-20 50 views
11

我是新來的android和一直在尋找一個解決方案,但迄今沒有運氣。我想創建一個如下圖所示的佈局。horizo​​ntalScrollView內的佈局,是屏幕的大小

我想有一個linearLayout是屏幕的大小。然後有另一個linearLayout也是屏幕的大小,但屏幕外。然後我可以在兩個虛擬「屏幕」之間滾動。

enter image description here

有講解如何擴展滾動視圖類,這樣我可以得到一個很酷的捕捉效果的有趣的文章,所以如果我能得到這個工作,我的應用程序會感覺像家之間滾動屏幕。

我已閱讀關於重量和關於scrollView的fillViewport =「true」。恐怕我不明白如何使用horizo​​ntalScrollView使linearLayouts填充屏幕。我試圖fill_parent和wrap_content的各種組合,但無濟於事。

正如我所看到的,只要我在屏幕變化的基礎上構建子視圖(每個「屏幕」中的元素),此功能不會損害具有不同屏幕的設備之間的應用程序的可移植性。

這裏是XML我嘗試的​​一個簡單的例子:

<HorizontalScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/HorizontalScrollView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:fillViewport="true"> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/txtTestBox" 
      > 
     </EditText> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Button 1" 
     /> 

    </LinearLayout> 

</LinearLayout> 

</HorizontalScrollView> 

不幸的是,甚至沒有接近我所期待的。希望這可以做...

感謝您的任何幫助或建議。

+0

我認爲將鏈接放置到我所指的'snap'代碼可能會有所幫助。它可以在這裏找到:http://blog.velir.com/index.php/2010/11/17/android-snapping-horizo​​ntal-scroll/ – paradoX 2010-12-20 14:10:42

+0

我希望實現的功能的另一個例子可以在Android的Firefox移動應用程序。沿着頁面左側的標籤欄(直到你滾動屏幕之外)似乎以某種方式創建了一個可滾動的表面,其子視圖是屏幕的確切尺寸(它會創建一個「屏幕」的物理電話屏幕,當用戶滾動時,他們可以查看他們的標籤和屏幕)。希望這能讓我的問題清楚。 – paradoX 2010-12-21 22:13:48

回答

1

水平滾動視圖可以無限地左右縮放,因此「填充父項」很可能不會像您期望的那樣工作在內部佈局。你有沒有明確指定內部佈局的寬度?

喜歡的東西:

<HorizontalScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/HorizontalScrollView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:fillViewport="true"> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="400dp" 
    android:layout_height="fill_parent"> 

..... 

</LinearLayout> 


</HorizontalScrollView> 
+0

這是我現在採用的方法。我希望避免手動格式化每個視圖,但似乎這是唯一的選擇。謝謝! – paradoX 2011-01-28 19:42:18

0

是否有一個特定的原因,你需要在其中一個虛擬屏幕2?爲什麼不把他們分開?您可以擴展GestureDetector,因此當出現滾動手勢時,會自動「捕捉」到下一個屏幕。

+0

好問題,我對此感興趣,因爲我認爲它會允許更直觀的界面。由於我的界面有點複雜,我認爲能夠「窺視」下一個屏幕上的內容將會很有用。此外,在這一點上,我想知道如何做到這一點,知道它可以做到這一點(我一直在反駁這個問題,我不想放棄)。如果我找不到答案,我打算回到你提議的事情上。 – paradoX 2010-12-20 14:06:29

+0

順便說一句,我所指的'快照'代碼的鏈接可以在這裏找到:http://blog.velir.com/index.php/2010/11/17/android-snapping-horizo​​ntal-scroll/ – paradoX 2010-12-20 14:09:00

+0

這不是問題的答案。希望有更好的答案。也許我可以在Firefox手機源代碼中找到答案... – paradoX 2010-12-21 22:15:41

1

See here的解決方案是使用android:fillViewport="true"ScrollView.沒有必要修復的LinearLayout寬度。

 <HorizontalScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:fillViewport="true" 
      android:orientation="vertical" 
      android:scrollbars="none"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
       //create your views 
      </LinearLAyout> 
</HorizontalScrollView> 
相關問題