android - 如何将自定义 View 放入自定义 viewGroup/Layout 中

标签 android user-interface android-custom-view

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

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

xml 代码,前 2 个 View 没有问题,但嵌套在 CustomLayout 中的第 3 个 View 没有显示,只有白色区域,里面的 View 不可见

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 的代码,与其他自定义 View 一样简单,它只是用颜色填充区域,它嵌套在 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) {

}

}

最佳答案

custom view inside of a custom viewGroup not visible, how can I get it to show up?

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

关于android - 如何将自定义 View 放入自定义 viewGroup/Layout 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22042215/

相关文章:

android - 在自定义 View 中处理后退按钮

android - 如何解决 Android Studio 3 上链接文件资源失败的错误?

c# - 在 C# 中访问 android 设备

c - 在新窗口中从 GTKEntry 获取整数

Android:是否可以在可绘制选择器中使用字符串/枚举?

安卓。 Canvas 缩放和平移

java - 如何在android上制作一个按下和释放的自定义按钮?

android - BottomSheet 在按钮点击时跳跃

ios - MPMediaPlayerController 永久隐藏用户界面元素

java - Color.getColor(字符串名称) 不起作用