通用itemview

来源:互联网 发布:鹰朗el01正品淘宝店 编辑:程序博客网 时间:2024/03/29 17:21
public class SettingItemView extends RelativeLayout {   private TextView tv_left;   private TextView tv_right;      private ImageView iv_left;   private ImageView iv_right;   private String left;   private String right;   private int image;   private int rightImage;   public SettingItemView(Context context, AttributeSet attrs) {      super(context, attrs);      TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.combinationView);      left = typedArray.getString(R.styleable.combinationView_left);      right = typedArray.getString(R.styleable.combinationView_right);      image = typedArray.getResourceId(R.styleable.combinationView_image, -1);      rightImage = typedArray.getResourceId(R.styleable.combinationView_rightImage, -1);      typedArray.recycle();      LayoutInflater.from(context).inflate(R.layout.setting_item_view1, this);      tv_left = (TextView) findViewById(R.id.tv_left);      tv_right = (TextView) findViewById(R.id.tv_right);      iv_left = (ImageView) findViewById(R.id.iv_left);      iv_right = (ImageView) findViewById(R.id.iv_right);      if (left != null) {         tv_left.setText(left);      }      if (right != null) {         tv_right.setText(right);      }      iv_left.setImageResource(image);      iv_right.setImageResource(rightImage);   }         public void setRightText(String str){      tv_right.setText(str);   }   public String getRightText(){            return tv_right.getText().toString();   }   public void setTextColor(int textColor) {      tv_left.setTextColor(textColor);   }}2.attrs.xml
<declare-styleable name="combinationView">    <attr name="left" format="string"/>    <attr name="right" format="string"/>    <attr name="image" format="reference"/>    <attr name="rightImage" format="reference"/>    <attr name="android:text" /></declare-styleable>

3.xml:
setting_item_view1

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="44dp" >    <ImageView        android:id="@+id/iv_left"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:src="@drawable/st_rename" />    <ImageView        android:id="@+id/iv_right"        android:layout_width="10dp"        android:layout_height="15dp"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:layout_margin="10dp"/>    <TextView        android:id="@+id/tv_left"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/iv_left"        android:layout_toRightOf="@+id/iv_left"        android:textSize="18dp"        android:textColor="@android:color/black" />    <TextView        android:id="@+id/tv_right"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/tv_left"        android:layout_toLeftOf="@+id/iv_right"        android:textSize="18dp"        android:textColor="@color/st_grey"/></RelativeLayout>

4.引用
<com.views.setting.SettingItemView    android:id="@+id/me_mysetting"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    andy:image="@drawable/me_mysetting"    andy:left="@string/st_personal_setting"//个人设置文体    andy:rightImage="@drawable/st_more" />