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

Android 码拜 7年前 (2017-04-30) 3376次浏览
下面是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 的构造函数中写的有问题
解决方案

10

运行就显示出来了

90

你确定,试过了,本人试了,没问题啊

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>

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明在布局文件中 引用自定义View,VIEW不显示
喜欢 (0)
[1034331897@qq.com]
分享 (0)