Please Enable JavaScript!
Mohon Aktifkan Javascript![ Enable JavaScript ]

[ANDROID] CUSTOM ATTRIBUTE RELATIVELAYOUT

2020. 12. 10. 11:36programming/android

728x90

custom attribute xml 설정 방법과 값 가져오기 

  • class
package com.polarglow.custom;
 
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
 
/** A RelativeLayout that will always be square -- same width and height,
 * where the height is based off the width. */
public class SquareRelativeLayout extends RelativeLayout {
 
    public SquareRelativeLayout(Context context) {
        super(context);
    }
 
    public SquareRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @TargetApi(VERSION_CODES.LOLLIPOP)
    public SquareRelativeLayout(Context context, AttributeSet attrs,         int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Set a square layout.
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
 
}

 

  • res > values > attrs.xml
<span style="font-family: verdana, geneva, sans-serif;"><resources>
 
    <!-- Declare custom theme attributes that allow changing which styles are
         used for button bars depending on the API level.
         ?android:attr/buttonBarStyle is new as of API 11 so this is
         necessary to support previous API levels. -->
    <declare-styleable name="RatioRelativeLayout">
        <attr name="ratio" format="float"></attr>
        <attr name="android_id" format="reference"></attr>
    </declare-styleable>
 
</resources></span>

 

  • res > layout > xml
<com.polarglow.custom.SquareRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:android_id="@+id/customRelative"
    app:ratio="1.7"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <!-- More widgets here. -->
 
</com.polarglow.custom.SquareRelativeLayout>

 

  • custom attribute value 가져오는 방법
 TypedArray ratioRelative;
    Float fRatio = 1.0f;
    RecyclerView recyclerView;        
    ratioRelative = context.obtainStyledAttributes(attrs,R.styleable.RatioRelativeLayout);
    fRatio = ratioRelative.getFloat(R.styleable.RatioRelativeLayout_ratio,0.4f);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

만약 xml에서 다음과 같이 사용하고자 할 때 DataBindingAdapter 설정을 해야 합니다.

ItemView.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
 
    <data>
        <variable
            name="ItemVO"
            type="com.polarglow.custom.item.ItemVO" />
     </data>
 
     <com.polarglow.custom.SquareRelativeLayout
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          app:ratio="@{ItemVO.type.equals(`New`) ? 1.0f : 2.0f}"
     </com.polarglow.custom.SquareRelativeLayout>
 
</layout>

DataBindingAdapters.class

@BindingAdapter({"ratio"})
public static void ratio(RatioRelativeLayout view, float ratio) {
    view.ratio = ratio;
}

app:ratio=”@{ItemVO.type.equals(New) ? 1.0f : 2.0f}” 이부분에서 3항 연산자를 통해서 수행하기 때문에

ratio가 어떤 값이며 어디에 있는지 알 수 없는 상태. 따라서 ratio를 초기화 및 할당해주는 작업이 필요합니다.

DataBindingAdapter가 필요합니다.

 

만약  app:ratio=”1.0″ 으로 한다면 DataBindingAdapter 가 필요 없습니다.

 

Continue to code. CTC.

728x90