41

これらの質問に関して:

ListView の TextView にグラデーション効果を追加すると、NPE が生成されます

ListView の色とフォントを変更する方法

グラデーション効果のTextViewあるの背景を設定する方法を知りたいですか?ListView

上記の質問の 1 つで、グラデーション効果をテキストに追加することになりましたTextView。そして、2 番目の質問に目を通した後、固定の背景色しか追加できないようです。

背景にグラデーションを追加するにはどうすればよいですか? 私は作るべきCustomListAdapterですか?

4

3 に答える 3

103

ドローアブル リソースを作成し (以下の例を参照)、ListItem 用に作成したレイアウトに追加するだけです。

ドローアブル (res\drawable フォルダー内 - 任意の名前 - ex の場合は listgrad.xml ) は次のようになります。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
      android:startColor="@color/gradient_start"
      android:endColor="@color/gradient_end"
      android:angle="-270" /> 
</shape>

次のコード スニペットのように、リスト アイテムのレイアウト (このために定義する layout.xml ファイル) に追加します。

<TextView
        android:id="@+id/ranking_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/list_grad"
        />
...
于 2013-01-16T17:56:51.000 に答える
2

ここから参照: Android で角の丸い ListView を作成するにはどうすればよいですか? (私はそれが非常に便利であることがわかりました。)

以下をファイル (たとえば gradient.xml) に追加し、(res/drawable/gradient.xml) ディレクトリに配置します。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <gradient 
         android:startColor="#SomeGradientBeginColor"
         android:endColor="#SomeGradientEndColor" 
         android:angle="270"/> 

    <corners 
         android:bottomRightRadius="7dp" 
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp" 
         android:topRightRadius="7dp"/> 
</shape> 

このファイルの作成が完了したら、次のいずれかの方法で背景を設定します。

コードを通して:listView.setBackgroundResource(R.drawable.customshape);

XML を介して、次の属性をコンテナー (例: LinearLayout または任意のフィールド) に追加するだけです。

android:background="@drawable/customshape"
于 2014-07-15T02:21:53.980 に答える