【发布时间】: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