CustomView не отображается в режиме разработки - PullRequest
0 голосов
/ 26 сентября 2018

У меня есть CustomView расширение от RelativeLayout.
I Я использую это CustomView в файле макета, но Android Studio в режиме разработки не показывает CustomView.в чем проблема?

koala_appbar.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="top"
    tools:context=".activity.basic.BaseActivity">

    <ImageView
        android:id="@+id/appbar_imgLogo"
        android:layout_width="@dimen/appBar_iconWidth"
        android:layout_height="@dimen/appBar_iconHeight"
        android:src="@drawable/logo"
        android:layout_marginTop="@dimen/appBar_iconTopMargin"
        android:layout_marginLeft="@dimen/appBar_iconLeftMargin"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/appbar_height"
        android:orientation="horizontal"
        android:layoutDirection="ltr"
        >

        <koala.android.customer.views.widget.CustomTextView
            android:id="@+id/appBar_txtTitle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center|right"
            android:text="@string/app_name_persian"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/TextSize_large"
            app:tvCustomFont="@string/baseFont"
            />

        <koala.android.customer.views.widget.CustomImageView
            android:id="@+id/appBar_imgBack"
            android:layout_width="@dimen/appbar_height"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/ic_arrow_right"
            app:ivDrawableTint="@color/colorPrimary"
            android:padding="@dimen/appBar_backPadding"
            />

    </LinearLayout>

</RelativeLayout>

KoalaAppBar.java:

public class KoalaAppBar extends RelativeLayout {


    private static final String DEFAULT_TITLE = "default";

    private static final int DEFAULT_TITLE_COLOR = ResourcesCompat.getColor(AppController.getContext().getResources(), R.color.colorPrimary , null);

    private static final float DEFAULT_TITLE_FONT_SIZE = AppController.getContext().getResources().getDimension(R.dimen.TextSize_small);


    protected CustomTextView txtTitle;

    protected CustomImageView imgBack;

    protected ImageView imgLogo;



    public KoalaAppBar(Context context) {
        super(context);
        init(context , null);
    }

    public KoalaAppBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context , attrs);
    }

    public KoalaAppBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context , attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public KoalaAppBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        if(context != null){
            findViews(context);
        }

        if(attrs != null){
            initAttribute(context,attrs);
        }
    }

    private void initAttribute(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KoalaAppBar);

        String title = a.getString(R.styleable.KoalaAppBar_appTitle);

        if(title == null || title.isEmpty()){
            title = DEFAULT_TITLE;
        }

        int titleColor = a.getColor(R.styleable.KoalaAppBar_appTitleColor, DEFAULT_TITLE_COLOR);

        float fontSize = a.getDimension(R.styleable.KoalaAppBar_appTitleFontSize, DEFAULT_TITLE_FONT_SIZE);

        boolean logoVisibility = a.getBoolean(R.styleable.KoalaAppBar_appLogoVisibility , true);


        initTitle(title , titleColor , fontSize);

        initLogo(logoVisibility);

        a.recycle();
    }

    private void initLogo(boolean logoVisibility) {
        if(logoVisibility){
            this.imgLogo.setVisibility(VISIBLE);
        }else{
            this.imgLogo.setVisibility(GONE);
        }
    }

    private void initTitle(String title, int titleColor, float fontSize) {
        this.txtTitle.setText(title);
        this.txtTitle.setTextColor(titleColor);
        this.txtTitle.setTextSize(fontSize);
    }

    private void findViews(Context context) {
        View rootView =
                LayoutInflater.from(context).inflate(
                        R.layout.koala_appbar,
                        this,
                        true
                );


        this.txtTitle   = rootView.findViewById(R.id.appBar_txtTitle);

        this.imgBack    = rootView.findViewById(R.id.appBar_imgBack);

        this.imgLogo = rootView.findViewById(R.id.appbar_imgLogo);
    }


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...