开发者

android image in listview that thinks "outside" the box

I want to have an ImageView that is part of a listview row item, but that appea开发者_开发技巧rs to both in and out of the row. is this possible? if so, how? many thanks in advance...

android image in listview that thinks "outside" the box


The answer is "not exactly," at least not explicitly in the way you mean, but you should be able to make your image appear to be outside the row by simply using a background image for the list items that looks like the edge of the list occurs a few pixels off from where it actually does. Then you can just right align the element you want to appear overflown, and add some right-hand padding/margins to the elements that you don't.


I have this working now and i didnt have to mess with photoshop at all. I did it all using android xml.

the way to do it is to use a <layer-list> element and instead of using without using padding, like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="6dip" android:right="6dip" android:left="6dip">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:bottomRightRadius="2dip"
            android:bottomLeftRadius="2dip" android:topLeftRadius="6dip"
            android:topRightRadius="6dip" />
        <solid android:color="@color/list_view_outline" />

    </shape>
</item>
<item android:top="8dip" android:right="8dip" android:left="8dip">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:bottomRightRadius="2dip"
            android:bottomLeftRadius="2dip" android:topLeftRadius="6dip"
            android:topRightRadius="6dip" />
        <solid android:color="@android:color/white" />

        <stroke android:width="1dip" android:color="#BDBDBD" />
    </shape>
</item>

then just use a standard row layout for the list row:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/feed_list_row_content">
<ImageView android:id="@+id/author_image"
    android:layout_width="44dip" android:layout_height="44dip"
    android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
    android:src="@drawable/no_photo" android:layout_marginLeft="10dip"
    android:layout_marginTop="10dip" />
<TextView android:layout_width="wrap_content"
    android:layout_marginTop="10dip" android:layout_height="wrap_content"
    android:layout_toRightOf="@id/author_image" android:id="@+id/feed_message_content"
    android:layout_toLeftOf="@+id/action_button" />

<ImageView android:id="@id/action_button" android:src="@drawable/row_action"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentRight="true" android:layout_marginTop="20dip" />

I then set the background in the custom list adapter getView() method as the top & bottom rows render differently from the middle rows.

if(position == 0)
        {
            holder.contentLayout.setBackgroundResource(R.drawable.top_row);
        } else if(position == posts.size() - 1)
        {
            holder.contentLayout.setBackgroundResource(R.drawable.bottom_row);
        } else {
            holder.contentLayout.setBackgroundResource(R.drawable.inner_row);
        }


You should be able to implement this using the android:clipChildren="false" and android:clipToPadding="false" on the immediate parent view, then android:clipChildren="false" on all the ancestor view groups and then put a negative margin on the image view; i.e. android:layout_marginLeft="-10dp".

It took me a while to understand how these work, especially android:clipChildren="false". Google says "Defines whether a child is limited to draw inside of its bounds or not.", the key point is that the 'bounds' of the child is not the bounds of the parent view group, but the bounds of the view as defined by it's layout attributes, that's why you need it all the way up the ancestor tree.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜