个人中心或者设置常用的布局

先上张效果图 如下:

//自定义代码类   ItemWidget.java
package com.aimeilife.sylive.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aimeilife.sylive.R;
import com.aimeilife.sylive.utils.DimensionUtility;

/**
* @author xiaobai 2016年11月8日 @todo( )
*/
public class ItemWidget extends LinearLayout implements View.OnTouchListener {
private ItemWidget _this;
private DisplayMetrics dm;
private TypedArray attr;
private ImageView im;
private TextView tv;
private TextView tv2;
private ImageView im_arr;
private View v;

public ItemWidget(Context context) {
super(context);
init(context);
}

public ItemWidget(Context context, AttributeSet attrs) {
super(context, attrs);
attr = context.obtainStyledAttributes(attrs, R.styleable.GroupItem);
init(context);
}

public ItemWidget(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
attr = context.obtainStyledAttributes(attrs, R.styleable.GroupItem);
init(context);
}

private synchronized void init(final Context context) {
_this = this;
// 设置背景选中效果
this.setOrientation(LinearLayout.VERTICAL);
this.setBackgroundResource(R.drawable.item_select_click);
LinearLayout ll = new LinearLayout(context);
dm = DimensionUtility.getDm(context);
im = new ImageView(context);
im.setImageResource(attr.getResourceId(R.styleable.GroupItem_imgSrc, 0));
// 左边是否有缩进 margin 默认有
boolean isindentation = attr.getBoolean(R.styleable.GroupItem_isindentation, false);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER_VERTICAL;
params.rightMargin = (int) (16 * dm.density);
if (isindentation)
params.leftMargin = (int) (14 * dm.density);
im.setLayoutParams(params);
im.setScaleType(ScaleType.CENTER_CROP);
ll.addView(im);

tv = new TextView(context);
tv.setText(attr.getString(R.styleable.GroupItem_textStr));
int titDesColor = attr.getColor(R.styleable.GroupItem_textStrColor,
context.getResources().getColor(R.color.c010101));
tv.setTextColor(titDesColor);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, attr.getInt(R.styleable.GroupItem_textSiz, 15));
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity=Gravity.CENTER_VERTICAL;
tv.setLayoutParams(params);
ll.addView(tv);

//
boolean isTextStrVal = attr.getBoolean(R.styleable.GroupItem_isTextStrVal, false);
if (isTextStrVal) {
tv2 = new TextView(context);
tv2.setText(attr.getString(R.styleable.GroupItem_textStrVal));
int titDesValColor = attr.getColor(R.styleable.GroupItem_textStrValColor,
context.getResources().getColor(R.color.c999999));
tv2.setTextColor(titDesValColor);
tv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, attr.getInt(R.styleable.GroupItem_textSizVal, 12));
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER_VERTICAL;
params.rightMargin = (int) (16 * dm.density);
tv2.setLines(1);
tv2.setEllipsize(TruncateAt.END);
tv2.setLayoutParams(params);
ll.addView(tv2);
}
boolean isArrow = attr.getBoolean(R.styleable.GroupItem_isArrow, false);
if (isArrow) {
im_arr = new ImageView(context);
im_arr.setImageResource(
attr.getResourceId(R.styleable.GroupItem_arrowImgSrc, R.drawable.icon_jiantou));
LayoutParams params_arr = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params_arr.gravity=Gravity.CENTER_VERTICAL;
im_arr.setLayoutParams(params_arr);
im_arr.setScaleType(ScaleType.CENTER_CROP);
ll.addView(im_arr);
}
ll.setOrientation(LinearLayout.HORIZONTAL);
this.addView(ll, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
boolean isline = attr.getBoolean(R.styleable.GroupItem_isline, true);
if (isline) {
v = new View(context);
params = new LayoutParams(LayoutParams.MATCH_PARENT, (int) (1 * dm.density));
v.setBackgroundColor(context.getResources().getColor(R.color.ce5e5e5));
v.setLayoutParams(params);
this.addView(v);
}
this.setClickable(true);
this.setLongClickable(true);
this.setFocusable(true);
attr.recycle();
}

// 设置item的描述
public ItemWidget setTextStr(String title) {
tv.setText(title);
return _this;
}

public ItemWidget getTextStr(String title) {
tv.getText();
return _this;
}

// 设置item的描述
public ItemWidget setTextStrVal(String title) {
if( tv2 == null ) return _this;
tv2.setText(title);
return _this;
}

public ItemWidget getTextStrVal(String title) {
tv2.getText();
return _this;
}
public ItemWidget setArrow(boolean isShow) {
if( im_arr != null ){
im_arr.setVisibility( isShow ? View.VISIBLE :View.GONE );
}
return _this;
}
//箭头替换并设置 点击事件
public ItemWidget setImArr( int resId, final OnClickListener listener) {
if ( resId > 0 ) {
im_arr.setImageResource(resId);
}
if (null != listener) {
im_arr.setOnClickListener(listener);
}
return _this;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
}

//自定义attrs.xml

<!-- 自定义item -->
<declare-styleable name="GroupItem">
    <attr name="imgSrc" format="reference|color" /><!-- 图片 -->
    <attr name="arrowImgSrc" format="reference|color" />
    <attr name="isindentation" format="boolean" /><!-- 缩进 -->
    <attr name="isline" format="boolean" />
    <attr name="isArrow" format="boolean" />
    <attr name="textStr" format="string" />
    <attr name="textStrColor" format="color" />
    <attr name="textSiz" format="integer" />
    <attr name="isTextStrVal" format="boolean" />
    <attr name="textStrVal" format="string" />
    <attr name="textSizVal" format="integer" />
    <attr name="textStrValColor" format="color" />
</declare-styleable>

使用实例:

记得根目录标签加入

xmlns:app="http://schemas.android.com/apk/res-auto"
<包名.ItemWidget
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    app:arrowImgSrc="@drawable/icon_jiantou"
    app:imgSrc="@drawable/icon_yijianfank"
    app:isArrow="true"
    app:isline="true"
    app:textStr="@string/user_feedback">
</包名.ItemWidget>

修改为自己得即可。

不明白的留言,谢谢