2011-02-03 123 views
0

我想在Horizo​​ntallScrollView上添加前景漸變。一切工作正常,但滾動梯度隨佈局移動後。下面是這個問題的截圖: appliaction開始後:http://img819.imageshack.us/img819/6622/horizontalscroll.jpg 和滾動後:http://img709.imageshack.us/img709/388/horizontalscroll2.jpgHorizo​​ntalScrollView上的前景

在XML主要佈局:

<RelativeLayout 
android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@layout/backgroundgradient" 
> 

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


<HorizontalScrollView android:id="@+id/ScrollView1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:scrollbars="none" 
        android:foreground="@layout/scrollforegrad" 
        android:background="@layout/scrollbgrgrad" 
        android:layout_below="@id/LayoutHeader" 
        > 

        <LinearLayout 
         android:id="@+id/ThemeContainer" 
         android:orientation="horizontal" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         /> 
</HorizontalScrollView> 

和梯度的xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainForegroundGrad" 

> 
    <gradient 
     android:startColor="@color/scrollForegroundSide" 
     android:centerColor="@color/scrollForegroundCenter" 
     android:endColor="@color/scrollForegroundSide" 
     android:angle="0" 
    /> 
    <corners android:radius="0dp" /> 
</shape> 

如果有人有這樣的問題或知道如何解決它,請分享:) 在此先感謝。

回答

1

我認爲你不能在Horizo​​ntalScrollView中使用前景。前景drawable是一個從FrameLayout繼承的特徵滾動視圖,看起來這個特徵支持並沒有在滾動視圖中實現。

但是,重寫Horizo​​ntalScrollView draw()方法可以在內容之上繪製drawable是很容易的。我甚至可以爲您提供一些來自我的項目的代碼片段來完成這件事。

public void draw(Canvas aCanvas) { 
    super.draw(aCanvas); 

    getDrawingRect(mDrawingRect); 

    Gravity.apply(Gravity.CENTER_VERTICAL|Gravity.LEFT, 
      mForegroundDrawable.getIntrinsicWidth(), 
      mForegroundDrawable.getIntrinsicHeight(), 
      mDrawingRect, mForegroundDrawableBounds); 

    mForegroundDrawableBounds.offset(ARROWS_MARGIN_SIDES, ARROWS_MARGIN_VERTICAL); 

    mForegroundDrawable.setBounds(mForegroundDrawableBounds); 
    mForegroundDrawable.draw(aCanvas);  
} 
相關問題