问题
In my project, I have to implement the following design. It is a list of images and text to display. I used a custom layout to inflate the rows.


I set the above background to the item page. I set the image background to transparent. But I didn't get the output as the image one. I get the following image from the url.

How can achieve the design as the first image.
And my item xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@drawable/list_item_background" >
<ImageView
android:id="@+id/hotelimage"
android:layout_width="200dp"
android:layout_height="100dp"
android:contentDescription="@string/img"
android:scaleType="fitXY"
android:background="#80000000"
/>
<TextView
android:id="@+id/hotelnametxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/hotelnametxt"
android:text="@string/plus"
android:textColor="@color/pink"
android:textSize="20sp" />
<ImageView
android:id="@+id/tick1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/hotelnametxt"
android:layout_marginLeft="80dp"
android:layout_marginTop="15dp"
android:contentDescription="@string/img"
android:src="@drawable/tick_mark_normal" />
<ImageView
android:id="@+id/tick2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/hotelnametxt"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/tick1"
android:contentDescription="@string/img"
android:src="@drawable/tick_mark_normal" />
<ImageView
android:id="@+id/tick3"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/hotelnametxt"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/tick2"
android:contentDescription="@string/img"
android:src="@drawable/tick_mark_normal" />
<ImageView
android:id="@+id/tick4"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/hotelnametxt"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/tick3"
android:contentDescription="@string/img"
android:src="@drawable/tick_mark_normal" />
<ImageView
android:id="@+id/tick5"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/hotelnametxt"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/tick4"
android:contentDescription="@string/img"
android:src="@drawable/tick_mark_normal" />
<TextView
android:id="@+id/dollar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hotelnametxt"
android:layout_marginLeft="180dp"
android:layout_marginTop="5dp"
android:textColor="@color/white"
android:textSize="28sp" />
<Button
android:id="@+id/Viewbt"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_below="@+id/hotelnametxt"
android:layout_marginLeft="270dp"
android:layout_marginTop="10dp"
android:text="@string/precios"
android:textSize="12sp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="65dp"
android:text="@string/direccion"
android:textColor="@color/pink"
android:textSize="12sp" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginLeft="80dp"
android:layout_marginTop="3dp"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="12sp" />
</RelativeLayout>
<View
android:id="@+id/dividerView"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/item"
android:background="@color/cyan" />
</RelativeLayout>
回答1:
try this:
final ImageView iv = new ImageView(this);
int[] colors = {
0x00000000, 0x66000000, 0xbb000000, 0xff000000, 0xff000000
};
Drawable[] layers = {
getResources().getDrawable(R.drawable.layer0),
new GradientDrawable(Orientation.LEFT_RIGHT, colors),
};
LayerDrawable ld = new LayerDrawable(layers);
iv.setImageDrawable(ld);
setContentView(iv);
btw, with LayeredDrawable you can even do some nice things like that:
final ImageView iv = new ImageView(this);
int[] colors = {
0x00000000, 0x66000000, 0xbb000000, 0xff000000, 0xff000000
};
GradientDrawable gd = new GradientDrawable(Orientation.LEFT_RIGHT, colors);
final ScaleDrawable sd = new ScaleDrawable(gd, Gravity.RIGHT | Gravity.FILL_VERTICAL, 1, 1);
Drawable[] layers = {
getResources().getDrawable(R.drawable.layer0),
sd,
};
LayerDrawable ld = new LayerDrawable(layers);
iv.setImageDrawable(ld);
setContentView(iv);
Runnable action = new Runnable() {
@Override
public void run() {
int lvl = sd.getLevel();
if (lvl < 10000) {
sd.setLevel(lvl + 1000);
iv.postDelayed(this, 40);
}
}
};
iv.postDelayed(action, 2000);
来源:https://stackoverflow.com/questions/21414119/fadded-edge-to-imageview-in-android