안드로이드 커스텀 위젯 예제


TextView 가 2개가 함께 있는 커스텀 위젯

TextView의 Text를 바꾸거나,

커스텀 위젯의 레이아웃 백그라운드를 변경하거나
커스텀 위젯의 클릭 이벤트를 받아 처리함.

setTag, getTag 이용하여 값 전달함.


<< 호출 예 , 여러번 호출시 >>

ZoneCustomView zcv[] = new ZoneCustomView[count];

for (int i = 0; i < zcv.length; i++) {

zcv[i] = new ZoneCustomView(this, i+"", mZoneClickListener);

zcv[i].setId(i);

zcv[i].setBackgroundResource(R.drawable.box_e957_11);

zcv[i].setClickable(true);

zcv[i].setTitle(i+" kkk");

if(i==0){

zcv[i].setBackground(R.drawable.e957_c0101);

}


FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

params.leftMargin = Integer.parseInt(zoneList.get(i).get("LEFT"));

params.topMargin = Integer.parseInt(zoneList.get(i).get("TOP"));

fl_furni_body.addView(zcv[i], params);


}

View.OnClickListener mZoneClickListener = new View.OnClickListener() {

@Override

public void onClick(View v) {

           String count = v.getTag(R.string.furni_msg_tag).toString(); 

           Toast.makeText(mContext, "count: "+count, Toast.LENGTH_SHORT).show();

}

};


<< 커스텀 위젯 >>

public class ZoneCustomView extends LinearLayout{


Context mContext;

View mView;

TextView mTvTitle;

TextView mTvSales;

LinearLayout mLlZoneCustom;

private View.OnClickListener mClickListener;

      private String mCount = "";

public ZoneCustomView(Context context, String count, View.OnClickListener clicklistener) {

super(context);

             mCount = count;

mClickListener = clicklistener;

init(context);

}


private void init(Context context) {

        this.mContext = context ;

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mView=layoutInflater.inflate(R.layout.vms_custom_zone ,this);  

//        mView.findViewById(R.id.ll_zone_custom).setOnClickListener(mClickListener);

mTvTitle = (TextView) findViewById(R.id.tv_zone_title);

mTvSales = (TextView) findViewById(R.id.tv_zone_sales);

mLlZoneCustom = (LinearLayout) findViewById(R.id.ll_zone_custom);

             mLlZoneCustom.setOnClickListener(mClickListener);

mLlZoneCustom.setTag(R.string.furni_msg_tag, mCount);

}

public void setTitle(String str){

mTvTitle.setText(str);

}


public void setSales(String str){

mTvSales.setText(str);

}

public void setBackground(int resid){

mLlZoneCustom.setBackgroundResource(resid);

}


}



<< xml 레이아웃: vms_custom_zone.xml >>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="240dp"

    android:layout_height="111dp"

    android:orientation="vertical" 

    android:background="@drawable/box_e957_11"

    android:id="@+id/ll_zone_custom"

    android:onClick="true"

    >

    <TextView 

        android:id="@+id/tv_zone_title"

        android:layout_width="197.5dp"

        android:layout_height="26dp"

        android:text="여성 컬렉션"

        android:textColor="#ffffff"

        android:textSize="24dp"

        android:layout_gravity="center_horizontal"

        

        android:layout_marginTop="16.5dp"

        />"

    <TextView 

        android:id="@+id/tv_zone_sales"

        android:layout_width="197.5dp"

        android:layout_height="33dp"

        android:text="3,000,000"

        android:textColor="#ffffff"

        android:textSize="30dp"

        android:layout_gravity="center_horizontal"

        android:layout_marginTop="9dp"

        />

</LinearLayout>


<< xml 레이아웃: strings.xml >>


<?xml version="1.0" encoding="utf-8"?>

<resources 

    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2" 

    xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation">


    <string name="furni_msg_tag">태그전달값</string>

    

</resources>

+ Recent posts