如何设置列表视图的背景吗? [英] How to set background of listview?

查看:113
本文介绍了如何设置列表视图的背景吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 在我的活动之一。我有一个是扩大创建了一个customview 的ImageView 。我想添加这个customview作为listview.As的背景,我们有两个功能 setBackGroundDrawable(可绘制D) setBackGroundResource(资源)。我不能使用这任何一个?_爱有什么好主意任何人我怎么能做到这一点?我搜查,但未能得到任何解决方案。

I have a ListView in one of my activity. I have one customview which is created extending ImageView.I want to add this customview as background of listview.As we have two functions setBackGroundDrawable(Drawable d) and setBackGroundResource(resource).I cant use any one of this ?

Anyone having any good idea how i can do this? I searched but could not get any solution.

推荐答案

编辑:我有一个教程

Edited : As I got a tutorial

当要动态画一些二维图形,一个ShapeDrawable对象可能会满足您的需求。随着 ShapeDrawable ,您可以通过编程绘制原始的形状和他们的风格以任何方式可想而知。

When you want to dynamically draw some two-dimensional graphics, a ShapeDrawable object will probably suit your needs. With a ShapeDrawable, you can programmatically draw primitive shapes and style them in any way imaginable.

一个ShapeDrawable是可绘制的扩展,所以你可以使用一个在以往的可绘制预期 - 也许是视图背景,用<一集href=\"http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable%28android.graphics.drawable.Drawable%29\"相对=nofollow> setBackgroundDrawable()。当然,你也可以绘制形状,自己的定制视图,将添加到您的布局,但是你请。因为ShapeDrawable有它自己的draw()方法,您可以创建查看一个子类的View.onDraw()方法中绘制ShapeDrawable。这里,不只是这个视图类的一个基本扩展,画一个ShapeDrawable作为景观:

A ShapeDrawable is an extension of Drawable, so you can use one where ever a Drawable is expected — perhaps for the background of a View, set with setBackgroundDrawable(). Of course, you can also draw your shape as its own custom View, to be added to your layout however you please. Because the ShapeDrawable has its own draw() method, you can create a subclass of View that draws the ShapeDrawable during the View.onDraw() method. Here's a basic extension of the View class that does just this, to draw a ShapeDrawable as a View:

public class CustomDrawableView extends View {
    private ShapeDrawable mDrawable;

    public CustomDrawableView(Context context) {
        super(context);

        int x = 10;
        int y = 10;
        int width = 300;
        int height = 50;

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.draw(canvas);
    }
}

在构造函数中,一个ShapeDrawable是定义为一个 OvalShape 。它然后给出一个颜色和形状的边界被设定。如果您没有设置界限,则形状将无法绘制,而如果你不设置颜色,它会默认为黑色。

In the constructor, a ShapeDrawable is defines as an OvalShape. It's then given a color and the bounds of the shape are set. If you do not set the bounds, then the shape will not be drawn, whereas if you don't set the color, it will default to black.

通过自定义视图定义,就可以得出你喜欢的任何方式。与上面的例子中,我们可以在活动编程绘制形状:

With the custom View defined, it can be drawn any way you like. With the sample above, we can draw the shape programmatically in an Activity:

CustomDrawableView mCustomDrawableView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCustomDrawableView = new CustomDrawableView(this);

    setContentView(mCustomDrawableView);
}

如果您想从XML布局,而不是从活动得出这个自定义绘制,那么CustomDrawable类必须重写<一个href=\"http://developer.android.com/reference/android/view/View.html#View%28android.content.Context,%20android.util.AttributeSet%29\"相对=nofollow>视图(上下文,AttributeSet中)构造,通过通胀率从XML实例化一个视图时被调用。然后,CustomDrawable元素添加到XML,像这样:

If you'd like to draw this custom drawable from the XML layout instead of from the Activity, then the CustomDrawable class must override the View(Context, AttributeSet) constructor, which is called when instantiating a View via inflation from XML. Then add a CustomDrawable element to the XML, like so:

<com.example.shapedrawable.CustomDrawableView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />

该ShapeDrawable类(像许多其他类型的可绘制在机器人.graphics.drawable 包),您可以用公共的方法定义绘制的各种属性。您可能需要调整一些特性包括Alpha透明度,彩色滤光片,抖动,不透明度和颜色。

The ShapeDrawable class (like many other Drawable types in the android.graphics.drawable package) allows you to define various properties of the drawable with public methods. Some properties you might want to adjust include alpha transparency, color filter, dither, opacity and color.

您还可以使用XML定义原始绘制形状。欲了解更多信息,请参阅形状可绘制在绘图资源文件的章节

You can also define primitive drawable shapes using XML. For more information, see the section about Shape Drawables in the Drawable Resources document.

试试吧:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.example.shapedrawable.CustomDrawableView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"/>
</RelativeLayout>

和设置您的自定义绘制 - 冷杉到ImageView的。

And set your custom draw-ables to ImageView.

希望这会工作。 :)

Hope this will work. :)

这篇关于如何设置列表视图的背景吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆