1

ListViewタブレットサイズの画面の左側にあるがあります。私の目標は、右側に境界線のある無地の背景を作成し、リスト要素に重複する背景を適用してその境界線を分割し、右側のビューの一部として表示されるようにすることでした。


ListViewの背景

別の質問でエミールが提案したように<layer-list>、ドローアブルを使用して正しい境界線を達成しました:

rightborder.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/black" />
        </shape>
    </item>
    <item android:right="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>
    </item>

</layer-list>

...そしてこれが適切な測定のためのListView定義です:

<ListView
    android:id="@+id/msglist"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:divider="@color/black"
    android:dividerHeight="1dp"
    android:background="@drawable/rightborder"
    android:paddingRight="0dip">
</ListView>
<!-- I added the android:paddingRight after reading something 
about shape drawables and padding, don't think it actually
did anything. -->

色で上書きしようとしています

目的の効果を実現するためにgetView、アダプターの機能に次のものを配置しました。

//If it's selected, highlight the background
if(position == mSelectedIndex)
    convertView.setBackgroundColor(R.color.light_gray);

else
    convertView.setBackgroundResource(0);

ただし、この方法を使用すると、ドローアブルの黒い境界線はListView表示されたままになり、背景の白い部分だけが灰色に置き換えられました。 スクリーンキャプチャは次のとおりです。

色の背景を通して表示される境界線


ドローアブルで修正

shape思い切って、割り当てていた色をドローアブルに置き換えました。

selectedmessage.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/light_gray" />
</shape>

getViewスニペット:

//If it's selected, highlight the background
if(position == mSelectedIndex)
    convertView.setBackgroundResource(R.drawable.selectedmessage);

else
    convertView.setBackgroundResource(0);

これにより、以下に示すように、目的の結果が得られます。

境界線が表示されなくなりました


質問:

要素の背景として長方形をListView割り当てるとビュー全体がカバーされるのに、色を割り当てると黒い境界線が透けて見えるのはなぜですか?動作していることを嬉しく思いますが、Androidがビューをこのようにレンダリングする理由を知りたいので、Androidがビューをレンダリングする方法について詳しく知ることができます。

その他の注意事項:

  • 違いがあれば、ストックのAndroid3.2エミュレーターでプロジェクトを実行しています。
  • 1つの手がかりは、色の背景がリソースlight_grayよりも暗くレンダリングされているように見えることです。light_gray shape
  • 私はそれが違いを生むとは思わないが、それlight_grayは:

    <color name="light_gray">#FFCCCCCC</color>

4

1 に答える 1

1

あなたはこれを行うことはできません:

 convertView.setBackgroundColor(R.color.light_gray);

setBackgroundColorはリソースIDを取りません:http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int)

したがって、期待どおりに機能しない偶発的な動作が発生します。

あなたがしなければならないでしょう:

 convertView.setBackgroundColor(getResources().getColor(R.color.light_gray);

http://developer.android.com/reference/android/content/res/Resources.html#getColor(int)

于 2012-06-24T22:19:35.733 に答える