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>