【问题标题】:how to put a custom view inside of a custom viewGroup / Layout如何将自定义视图放在自定义视图组/布局中
【发布时间】:2014-02-26 13:01:02
【问题描述】:

自定义 viewGroup 内的自定义视图不可见,我怎样才能让它显示出来? 还是有更好的方法可以做到这一点?

没有编译或运行时错误,但视图没有显示在 viewGroup 中,它应该像其他视图一样用颜色填充该区域,但它是白色的,并且视图的颜色没有显示在 CustomLayout 内

xml 代码,前 2 个视图显示没有问题,但嵌套在 CustomLayout 内的第 3 个视图不显示,只是白色区域,内部视图不可见

CustomViewOne是一个单独的类文件,CustomViewTwo和CustomViewThree都嵌套在MainActivity类里面作为静态内部类,CustomLayout是一个单独的文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<com.example.customviewexample.CustomViewOne
    android:layout_width="100dp"
    android:layout_height="50dp" />

<view 
    class="com.example.customviewexample.MainActivity$CustomViewTwo"
    android:layout_width="100dp"
    android:layout_height="50dp" />

<com.example.customviewexample.CustomLayout
    android:layout_width="100dp"
    android:layout_height="50dp">

    <view 
        class="com.example.customviewexample.MainActivity$CustomViewThree"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />

</com.example.customviewexample.CustomLayout>

</LinearLayout>

这里是 CustomViewThree 的代码,与其他自定义视图一样简单,它只是用颜色填充区域,它嵌套在 MainActivity 内,因此您必须使用 MainActivity$CustomViewThree 来访问它。

public static class CustomViewThree extends View {

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

}

public CustomViewThree(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public CustomViewThree(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    canvas.drawColor(Color.GREEN);
}

}

这是 CustomLayout 类的代码

public class CustomLayout extends FrameLayout {

public CustomLayout(Context context) {
    super(context);
   init(context);
}

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

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

public void init(Context context) {

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

}

}

【问题讨论】:

  • 你应该发布CustomLayout的代码。
  • 刚刚按要求添加了
  • 我没有看到你在 CustomLayout 中调用 super.onLayout。

标签: android user-interface android-custom-view


【解决方案1】:

自定义视图组内的自定义视图不可见,我该如何获取它 出现?

包裹孩子的父母CustomLayout 有一个空的onLayout() 方法,使孩子不会出现。此方法在ViewGroup 中很重要,因为小部件使用它来将其子项放入其中。因此,您需要为此方法提供一个实现来放置子级(通过在每个具有适当位置的子级上调用layout() 方法)。由于CustomLayout 扩展了FrameLayout,你可以调用super 方法来使用FrameLayout 的实现,甚至更好地移除被覆盖的方法(有实现它的理由吗?)。

【讨论】:

  • 没错,onLayout() 方法是问题所在,我刚刚添加了这一行,现在它显示出来了,super.onLayout(changed, l, t, r, b);
  • @Kevik 当onLayout 是一个抽象方法时,你是如何调用它的?此外,当调用super.layout 时,您将收到StackOverflowError 异常
  • @Eido95 ViewGroup 类声明了 onLayout() 方法抽象。如果您直接扩展 ViewGroup,那么您必须 实现该方法,因为没有使用 super.onLayout() 调用的实现。在 Kevik 的案例中,他扩展了 FrameLayout,它提供了 onLayout() 方法的实现,因此您可以使用 super....
猜你喜欢
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
相关资源
最近更新 更多