사용자 정의 컨트롤(25) - 사용자 정의 컨트롤의 조합 컨트롤
우선 메인 레이아웃 파일을 보십시오
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:setting="http://schemas.android.com/apk/res/com.example.mycustomeitem"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.example.mycustomeitem.CustomeItem
android:id="@+id/zhuti"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_height"
android:layout_marginTop="10dp"
setting:Divider="@color/diverColor"
setting:TopDivider="@color/diverColor"
setting:content="@string/zhuti"
setting:contentColor="@color/contentColor"
setting:contentSize="@dimen/contentSize"
setting:leftImg="@drawable/zhuti"
setting:leftImgSize="@dimen/leftImgSize"
setting:rightImg="@drawable/arrow_right"
setting:rightImgSize="@dimen/rightImgSize" />
<com.example.mycustomeitem.CustomeItem
android:id="@+id/contact"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_height"
setting:Divider="@color/diverColor"
setting:content="@string/contact"
setting:contentColor="@color/contentColor"
setting:contentSize="@dimen/contentSize"
setting:leftImg="@drawable/contact"
setting:leftImgSize="@dimen/leftImgSize"
setting:rightImg="@drawable/arrow_right"
setting:rightImgSize="@dimen/rightImgSize" />
<com.example.mycustomeitem.CustomeItem
android:id="@+id/setting"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_height"
setting:BottomDivider="@color/diverColor"
setting:content="@string/setting"
setting:contentColor="@color/contentColor"
setting:contentSize="@dimen/contentSize"
setting:leftImg="@drawable/shezhi"
setting:leftImgSize="@dimen/leftImgSize"
setting:rightImg="@drawable/arrow_right"
setting:rightImgSize="@dimen/rightImgSize" />
</LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomeItem">
<attr name="leftImg" format="reference"></attr>
<attr name="leftImgSize" format="dimension"></attr>
<attr name="rightImg" format="reference"></attr>
<attr name="rightImgSize" format="dimension"></attr>
<attr name="content" format="string"></attr>
<attr name="contentSize" format="dimension"></attr>
<attr name="contentColor" format="color"></attr>
<attr name="BottomDivider" format="color"></attr>
<attr name="Divider" format="color"></attr>
<attr name="TopDivider" format="color"></attr>
</declare-styleable>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="contentColor">#000000</color>
<color name="diverColor">#FFB6C1</color>
<color name="pressed_color">#DA70D6</color>
<color name="normal_color">#FFFFFF</color>
</resources>
dimens.xml
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="item_height">50dp</dimen>
<dimen name="leftImgSize">20dp</dimen>
<dimen name="rightImgSize">15dp</dimen>
<dimen name="contentSize">16dp</dimen>
<dimen name="commonMargin">10dp</dimen>
<dimen name="dividerHeight">1dp</dimen>
</resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyCustomeItem</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="zhuti"> </string>
<string name="contact"> </string>
<string name="setting"> </string>
</resources>
shape_item.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@color/pressed_color"/>
</shape>
shape_item_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/normal_color"/>
</shape>
CustomeItem
package com.example.mycustomeitem;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomeItem extends RelativeLayout implements OnTouchListener {
private static final int LEFT_IMG_FLAG = 0x123;
private static final int CONTENT_FLAG = 0x234;
private Context context;
private int leftImg;
private int rightImg;
private int leftImgSize;
private int rightImgSize;
private int contentSize;
private String content;
private int contentColor;
private int commonMargin;
private int divider;
private int topDivider;
private int bottomDivider;
private int dividerColor;
private int dividerHeight;
public interface ItemClick{
void onClick();
}
private ItemClick itemClick;
public void setItemClick(ItemClick itemClick) {
this.itemClick = itemClick;
}
@SuppressLint("ResourceAsColor") @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.setBackgroundResource(R.drawable.shape_item);
Log.i("TAG", "down");
break;
case MotionEvent.ACTION_UP:
this.setBackgroundResource(R.drawable.shape_item_normal);
// this.setBackgroundResource(R.drawable.item_selector1);
Log.i("TAG", "up");
if (itemClick !=null) {
itemClick.onClick();
}
break;
default:
break;
}
return true;
}
public CustomeItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
initRes(attrs);
addViews();
this.setOnTouchListener(this);
}
public CustomeItem(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public CustomeItem(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
/**
* <attr name="leftImg" format="reference"></attr> <attr name="rightImg"
* format="reference"></attr>
*
* <attr name="leftImgSize" format="dimension"></attr> <attr
* name="rightImgSize" format="dimension"></attr> <attr name="content"
* format="string"></attr> <attr name="contentSize"
* format="dimension"></attr> <attr name="contentColor"
* format="color"></attr>
*
* @param attrs
*/
private void initRes(AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.CustomeItem);
leftImg = ta.getResourceId(R.styleable.CustomeItem_leftImg, -1);
leftImgSize = ta.getDimensionPixelOffset(
R.styleable.CustomeItem_leftImgSize, -1);
rightImg = ta.getResourceId(R.styleable.CustomeItem_rightImg, -1);
rightImgSize = ta.getDimensionPixelOffset(
R.styleable.CustomeItem_rightImgSize, -1);
contentSize = ta.getDimensionPixelOffset(
R.styleable.CustomeItem_contentSize, -1);
content = ta.getString(R.styleable.CustomeItem_content);
contentColor = ta.getColor(R.styleable.CustomeItem_contentColor, -1);
divider = ta.getColor(R.styleable.CustomeItem_Divider, -1);
topDivider = ta.getColor(R.styleable.CustomeItem_TopDivider, -1);
bottomDivider = ta.getColor(R.styleable.CustomeItem_BottomDivider, -1);
ta.recycle();
commonMargin = (int) getResources().getDimension(R.dimen.commonMargin);
dividerColor = getResources().getColor(R.color.diverColor);
dividerHeight = (int) getResources()
.getDimension(R.dimen.dividerHeight);
}
private void addViews() {
if (divider != -1) {
View divider = new View(context);
divider.setBackgroundColor(dividerColor);
LayoutParams dividerParams = new LayoutParams(
LayoutParams.FILL_PARENT, dividerHeight);
dividerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
dividerParams.addRule(RelativeLayout.ALIGN_START, CONTENT_FLAG);
divider.setLayoutParams(dividerParams);
addView(divider);
}
if (topDivider != -1) {
View divider = new View(context);
divider.setBackgroundColor(dividerColor);
LayoutParams dividerParams = new LayoutParams(
LayoutParams.FILL_PARENT, dividerHeight);
dividerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
// dividerParams.addRule(RelativeLayout.ALIGN_START, CONTENT_FLAG);
divider.setLayoutParams(dividerParams);
addView(divider);
}
if (bottomDivider != -1) {
View divider = new View(context);
divider.setBackgroundColor(dividerColor);
LayoutParams dividerParams = new LayoutParams(
LayoutParams.FILL_PARENT, dividerHeight);
dividerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
// dividerParams.addRule(RelativeLayout.ALIGN_START, CONTENT_FLAG);
divider.setLayoutParams(dividerParams);
addView(divider);
}
//
ImageView itemLeftImg = new ImageView(context);
if (leftImg != -1) {
itemLeftImg.setBackgroundResource(leftImg);
}
itemLeftImg.setId(LEFT_IMG_FLAG);
RelativeLayout.LayoutParams itemLeftParams = new LayoutParams(
leftImgSize, leftImgSize);
itemLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
RelativeLayout.TRUE);
itemLeftParams.addRule(RelativeLayout.CENTER_VERTICAL,
RelativeLayout.TRUE);
itemLeftParams.leftMargin = commonMargin;
itemLeftImg.setLayoutParams(itemLeftParams);
this.addView(itemLeftImg);
//
TextView itemContent = new TextView(context);
itemContent.setText(content);
itemContent.setTextColor(contentColor);
itemContent.setId(CONTENT_FLAG);
// int contentSizePx = PhoneUtils.dp2px(context, contentSize);
// Log.i("TAG", "contentSizePx--"+contentSizePx);
itemContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
LayoutParams contentParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
contentParams.addRule(RelativeLayout.CENTER_VERTICAL,
RelativeLayout.TRUE);
contentParams.addRule(RelativeLayout.RIGHT_OF, LEFT_IMG_FLAG);
contentParams.leftMargin = commonMargin;
itemContent.setLayoutParams(contentParams);
this.addView(itemContent);
// rightImag
ImageView itemRightImg = new ImageView(context);
if (rightImg != -1) {
itemRightImg.setBackgroundResource(rightImg);
}
RelativeLayout.LayoutParams itemRightParams = new LayoutParams(
rightImgSize, rightImgSize);
itemRightParams.rightMargin = commonMargin;
itemRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
RelativeLayout.TRUE);
itemRightParams.addRule(RelativeLayout.CENTER_VERTICAL,
RelativeLayout.TRUE);
itemRightImg.setLayoutParams(itemRightParams);
this.addView(itemRightImg);
}
}
MainActivity
package com.example.mycustomeitem;
import com.example.mycustomeitem.CustomeItem.ItemClick;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomeItem zhuti = (CustomeItem) findViewById(R.id.zhuti);
zhuti.setItemClick(new ItemClick() {
@Override
public void onClick() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, " 。。", 0).show();
}
});
}
}
PhoneUtils
package com.example.mycustomeitem;
import android.content.Context;
public class PhoneUtils {
public static int dp2px(Context context,float dpValue){
float scale = context.getResources().getDisplayMetrics().density;
return (int)(dpValue * scale +0.5f);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.