【问题标题】:How to make custom view ( imageview + progressbar)?如何制作自定义视图(imageview + progressbar)?
【发布时间】:2014-08-29 21:28:06
【问题描述】:

我想在 imageView 上显示进度条。如果我这样做,通常效果很好:

<RelativeLayout
        android:id="@+id/image_holder"
        android:layout_width="120dip"
        android:layout_height="120dip"
        android:layout_gravity="center"
        android:layout_marginLeft="16dip"
        android:layout_marginRight="16dip"
        android:layout_marginTop="22dip" >

        <ImageView
            android:id="@+id/im_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop" />

        <ProgressBar
            android:id="@+id/progressbar1"
            android:layout_width="match_parent"
            android:visibility="gone"
            android:layout_height="match_parent" />
    </RelativeLayout>

是否可以将其组合成单个小部件?

【问题讨论】:

标签: android


【解决方案1】:

是的,你可以,只需创建一个扩展 RelativeLayout 的新类并像这样膨胀它:

public class MyView extends RelativeLayout {
    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();

    }

    private void init() {
        inflate(getContext(), R.layout.myLayout, this);
        //Create your layout here
    }
}

【讨论】:

  • 这样你最终会在视图层次结构中有两个RelativeLayouts
  • 是的。如果这是一个问题,您可以使用 View 而不是 RelativeLayout。
  • 实际上是ViewGroup(例如FrameLayout),而不是View as View不能有子View,但仍然是层次结构中的一个额外View
  • 你有完整的解决方案吗?
猜你喜欢
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多