在布局文件中 引用自定义View,VIEW不显示

Mr_Suntb 2017-04-17 10:13:02
下面是main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv_view_lockscreen_foreground"
android:background="#FFC5D0FD"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- time-->
<TextView android:text="12:00"
android:id="@+id/tv_view_lockscreen_foreground_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="34dp"
android:textColor="#FFFFFFFF"
android:textSize="66.7sp" />

<com.handarui.catui.dao.CatViewLockImpl
android:contentDescription="catView"
android:id="@+id/rv_view_lockscreen_catcomb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#44D0FD">
</com.handarui.catui.dao.CatViewLockImpl>

</RelativeLayout>

在com.handarui.catui.dao.CatViewLockImpl中的构造函数中

mCatCombView=inflate(mContext, R.layout.view_catcomb, null);
this.addView(mCatCombView);

view_catcomb.xml是专门针对CatViewLockImpl类写的布局文件。里面有一些图片


这样当我在MainAcitivity中 setContentView(R.layout.main)的时候。TextView显示正常。但是CatViewLockImpl的图片并未显示出来。谁能指点下问题。

我怀疑是 CatViewLockImpl 的构造函数中写的有问题
...全文
1267 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
abandon_asdn 2019-06-28
  • 打赏
  • 举报
回复
对于布局的引用,不知道有没有什么说法,比如线性布局中可以引用什么布局,相对布局中只能引用相对布局? 本人接触android开发不久,问这个问题是因为最近我在练习时,在一个相对布局中include一个线性布局后,在程序运行后没有显示,但是把父布局改为线性布局后,可以看到效果才想到问一问的,有知道的朋友麻烦解释一下,谢谢了
assky124 2017-04-18
  • 打赏
  • 举报
回复
没赋值,就在自定控件中,添上这一句。 你的自定义View已经在Main XML中实例过了,你又inflate 等于又new了一个
Mr_Suntb 2017-04-18
  • 打赏
  • 举报
回复
引用 5 楼 assky124 的回复:
View.inflate(context, R.layout.view_catcomb, this); 构造函数中 试试
mCatCombView=inflate(mContext, R.layout.view_catcomb, this); 同样的效果 。如果我在 MainActivity中 SetConetnView(R.layout.view_catcomb).是OK 的。说明view_catcomb.xml是OK的 这个不需要重写onLayOut()吧,我的布局是固定的了。
assky124 2017-04-18
  • 打赏
  • 举报
回复
View.inflate(context, R.layout.view_catcomb, this); 构造函数中 试试
Mr_Suntb 2017-04-18
  • 打赏
  • 举报
回复
引用 3 楼 assky124 的回复:
你确定你处理过图片显示了? onDraw中绘制图片了么?
onDraw中没有做任何事情 ,图片都是写在了xml中 在com.handarui.catui.dao.CatViewLockImpl中的构造函数中,我怀疑就是对自定义VIEW和根VIEW的关系不清楚 mCatCombView=inflate(mContext, R.layout.view_catcomb, null); this.addView(mCatCombView);
assky124 2017-04-18
  • 打赏
  • 举报
回复
你确定你处理过图片显示了? onDraw中绘制图片了么?
Mr_Suntb 2017-04-18
  • 打赏
  • 举报
回复
运行也不显示,我尝试吧CatViewLockImpl 设置了大小和背景颜色,运行的时候是显示一个有颜色的方块。这说明这个自定义的VIEW 是显示的,只是其内部的图片并没有显示。 我尝试 使用 setContentView(new CatViewLockImpl(context)); 也不会显示
lixuce1234 2017-04-18
  • 打赏
  • 举报
回复
运行就显示出来了
Mr_Suntb 2017-04-18
  • 打赏
  • 举报
回复
问题解决了,可能是我的自定义VIEW不是继承于ReleativeLayout而是继承于 ViewGroup 。大概确定是这个问题。 过程中,布局文件总是提示错误 java.lang.NoClassDefFoundError: Could not initialize class ,**** Use View.isInEditMode()***对我造成了很大误导,我一直以为是有错误。这个是因为可视化编辑器无法识别自定义控件。 但是实际这个错误可以不考虑。 当然一直不显示我的VIEW的问题,应该还是因为继承的问题。
Mr_Suntb 2017-04-18
  • 打赏
  • 举报
回复
引用 9 楼 assky124 的回复:
你确定,试过了,我试了,没问题啊

public class EditTextEx extends LinearLayout {

    public EditTextEx(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.v_edit_text_ex, this);
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/imageBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_create_black_24dp" />
</LinearLayout>
我测试你的是没问题,我把你的 布局文件替换为 R.layout.view_catcomb 也是能正常显示的。 现在我怀疑是我自定义的VIEW代码有问题。多谢你了,一会我结帖,把分数给你。非常感谢
assky124 2017-04-18
  • 打赏
  • 举报
回复
你确定,试过了,我试了,没问题啊

public class EditTextEx extends LinearLayout {

    public EditTextEx(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.v_edit_text_ex, this);
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/imageBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_create_black_24dp" />
</LinearLayout>
Mr_Suntb 2017-04-18
  • 打赏
  • 举报
回复
引用 7 楼 assky124 的回复:
没赋值,就在自定控件中,添上这一句。 你的自定义View已经在Main XML中实例过了,你又inflate 等于又new了一个
我这个inflate是自定义VIEW 加载它自己的布局文件 。这个布局文件并未在main.xml中。 也就是说再 自定义VIEW 被NEW的时候,它是要初始化自己的内容的(都是图片),这个内容来自一个独立的布局文件

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧