1
我正在嘗試使用圖像,透明漸變疊加和textView來實現一個gridView,因此無論圖像的顏色如何,文本總是可讀的。我無法添加疊加層和TextView。我在另一個問題中讀到,RelativeLayouts是疊加的方式,然後爲漸變XML文件設置RelativeLayout背景。這是我到目前爲止。以編程方式覆蓋圖像
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
public GridViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
RelativeLayout gradient;
TextView textView;
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(screenWidth/2, screenWidth/2));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
gradient = new RelativeLayout(mContext);
gradient.setBackground(mContext.getResources().getDrawable(R.drawable.gridview_gradient_image_overlay));
gradient.setLayoutParams(new GridView.LayoutParams(screenWidth/2, screenWidth/2));
} else {
imageView = (ImageView) convertView;
gradient = (RelativeLayout) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to images
private Integer[] mThumbIds = {
R.drawable.dog, R.drawable.cat,
R.drawable.horse, R.drawable.bunny,
R.drawable.fish, R.drawable.hamster,
R.drawable.hedgehog, R.drawable.bird,
R.drawable.turtle, R.drawable.goat
};
}
這是梯度XML文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#33000000"
android:centerColor="#11000000"
android:endColor="#00000000"
android:angle="90"
android:dither="true"
/>
</shape>
XML佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlGradient"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gridview_gradient_image_overlay">
<TextView
android:id="@+id/tvGridViewAnimal"
android:layout_width="10dp"
android:layout_height="10dp"
android:textColor="#ffffff"/>
</RelativeLayout>
不能使用谷歌? –
這是一個很常見的問題 –
發佈您的XML佈局,這是一個z順序問題 – bonnyz