自定義的ViewGroup中添加自定義View 造成的無法顯示問題(個人)


 首先說一下我在網上找了很久沒有找到說明方法所以我就自己試着寫了一下

1、我自定義了一個繼承了RelativeLayout的ViewGroup

java代碼如下:

/**
 * 簡單自定義三頭像
 * @author M.Z
 */
public class MyGroupHeaderImg extends RelativeLayout {

    
    public MyGroupHeaderImg(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mView = inflate(context, R.layout.view_group_head_img,this);
        initView();
    }
    
    public MyGroupHeaderImg(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    private Context mContext;
    
    private CircleProgressBar firstImg,scendImg,threeImg;
    
    private View mView;
    /**
     *  初始化
     */
    private void initView() {
        firstImg = (CircleProgressBar) mView.findViewById(R.id.first_ivAvator);
        scendImg = (CircleProgressBar) mView.findViewById(R.id.scend_ivAvator);
        threeImg = (CircleProgressBar) mView.findViewById(R.id.three_ivAvator);
    }
    
    /**
     * 設置Url
     * @param avatorUrls
     */
    public void setAvatorArray(String[] avatorUrls){
        
        VocImageLoader.getInstance().displayImage(avatorUrls[0],
                firstImg, MyDisplayImageOptions.getAvatorImageOption(),
                null, null);
        
        VocImageLoader.getInstance().displayImage(avatorUrls[1],
                scendImg, MyDisplayImageOptions.getAvatorImageOption(),
                null, null);
        
        VocImageLoader.getInstance().displayImage(avatorUrls[2],
                threeImg, MyDisplayImageOptions.getAvatorImageOption(),
                null, null);
    }
}

 xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:circleBar="http://schemas.android.com/apk/res/com.iflytek.vocation_edu_cloud"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@android:color/transparent">

    <com.iflytek.voc_edu_cloud.view.CircleProgressBar
        android:id="@+id/scend_ivAvator"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignRight="@+id/first_ivAvator"
        android:layout_alignTop="@+id/first_ivAvator"
        android:layout_marginRight="19dp"
        android:layout_marginTop="32dp"
        android:contentDescription="@string/app_name"
        circleBar:border_color="@color/white"
        circleBar:border_progress="100"
        circleBar:border_width="1dp"
        circleBar:inner_border_color="#ffffff"
        circleBar:one_src="@drawable/defult_avatar" />

    <com.iflytek.voc_edu_cloud.view.CircleProgressBar
        android:id="@+id/three_ivAvator"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/first_ivAvator"
        android:layout_alignTop="@+id/scend_ivAvator"
        android:layout_marginLeft="21dp"
        android:contentDescription="@string/app_name"
        circleBar:border_color="@color/white"
        circleBar:border_progress="100"
        circleBar:border_width="1dp"
        circleBar:inner_border_color="#ffffff"
        circleBar:one_src="@drawable/defult_avatar" />

    <com.iflytek.voc_edu_cloud.view.CircleProgressBar
        android:id="@+id/first_ivAvator"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/app_name"
        circleBar:border_color="@color/white"
        circleBar:border_progress="100"
        circleBar:border_width="1dp"
        circleBar:inner_border_color="#ffffff"
        circleBar:one_src="@drawable/defult_avatar" />

</RelativeLayout>

 因為我布局自定義的ViewGroup中包含了一個自定義的View,而且有個xmlns:circleBar="http://schemas.android.com/apk/res/com.iflytek.vocation_edu_cloud"

導致我在調用的時候 是一片空白調用的xml如下:

<com.iflytek.voc_edu_cloud.view.MyGroupHeaderImg
       android:id="@+id/member_item_ivAvator"
       android:layout_width="80dp"
       android:layout_height="80dp"
       android:layout_alignParentLeft="true"
       android:contentDescription="@string/app_name"
   />

 

 

解決方法:

  要解決不顯示的問題也很簡單就是在調用的地方加上這句  xmlns:circleBar="http://schemas.android.com/apk/res/com.iflytek.vocation_edu_cloud"如下:

<com.iflytek.voc_edu_cloud.view.MyGroupHeaderImg
         xmlns:circleBar="http://schemas.android.com/apk/res/com.iflytek.vocation_edu_cloud"
         android:id="@+id/member_item_ivAvator"
         android:layout_width="80dp"
         android:layout_height="80dp"
         android:layout_alignParentLeft="true"
         android:contentDescription="@string/app_name"
         />

 之后就可以正常的使用了.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM