2013-04-21 105 views
0

我有一個TextView覆蓋在圖像上,用作背景的漸變。然而,正如你在圖像中看到的那樣,出於某種原因,TextView使用android:startColor顏色作爲純色背景。TextView忽略漸變 - Android

enter image description here

Gradient.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:endColor="#00000000" 
    android:startColor="#FF999999" 
    android:angle="90"/>  
</shape> 

的onCreate在MainActivity

View overlay = (View) findViewById(R.id.overlay); 
    overlay.setBackgroundResource(R.drawable.gradient); 
    FrameLayout.LayoutParams params = 
     new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 250); 
    params.gravity = Gravity.BOTTOM; 
    overlay.setLayoutParams(params); 
    overlay.invalidate(); // update the view 

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#999999" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 
<FrameLayout 
android:layout_width="match_parent" 
android:layout_height="250dp" 
> 
<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:scaleType="fitXY" 
    android:src="@drawable/statement_header" /> 
<View 
    android:id="@+id/overlay" 
    android:layout_width="fill_parent" 
    android:layout_height="100dp" 
    /> 

<TextView android:id="@+id/tvHeader" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="20sp" 
      android:textStyle="bold" 
      android:text="Our Story" 
      android:textColor="#FFFFFF" 
      android:background="#00000000" 
      android:maxLines="1" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_gravity="bottom" android:layout_alignParentBottom="true" 
      /> 
</FrameLayout> 

回答

0

試試這個

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#999999" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 
<FrameLayout 
android:layout_width="match_parent" 
android:layout_height="250dp" 
> 
< ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:scaleType="fitXY" 
    android:src="@drawable/statement_header" /> 
< View 
    android:id="@+id/overlay" 
    android:layout_width="fill_parent" 
    android:layout_height="100dp" 
    android:background="@android:color/transparent" 
    /> 

< TextView android:id="@+id/tvHeader" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="20sp" 
      android:textStyle="bold" 
      android:text="Our Story" 
      android:textColor="#FFFFFF" 
      android:background="@drawable/gradient" 
      android:maxLines="1" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_gravity="bottom" 
      android:layout_alignParentBottom="true" 
      /> 
</FrameLayout> 

不需要更改代碼的背景只是嘗試使用此佈局。如果沒有必要刪除View孩子

+0

可悲的是,這只是改變了背後的實際文本背後,其餘的視圖仍然有同樣的問題。我在想,Android專門阻止你試圖做我想做的事情。也許是由於我測試的Android 2.3版本 – KingFu 2013-04-22 08:16:29