自定义View在开发中经常用得到,可以优化内存自定义效果等,下面实现一个类listview的效果。
先在values目录下新建一个attrs.xml的资源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="leftImage" format="reference"/>
<attr name="myText" format="string"/>
<attr name="rightImage" format="reference"/>
</declare-styleable>
</resources>
写出自定义view的xml文件
<?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="50dp">
<ImageView
android:id="@+id/iv_left"
android:layout_width="30dp"
android:layout_height="35dp"
android:src="@drawable/leftimage01"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_mytext"
android:text="联系客服"
android:layout_toRightOf="@id/iv_left"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:textSize="20sp"/>
<ImageView
android:layout_width="30dp"
android:layout_height="33dp"
android:id="@+id/iv_right"
android:src="@drawable/rightimage"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"/>
</RelativeLayout>
定义一个类,实现资源文件和xml文件的绑定
package demo.jim.com.myview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Administrator on 2015/7/24.
*/
public class CustomView extends FrameLayout {
private ImageView leftImage;
private TextView myText;
private ImageView rightImage;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
//注意inflate的第二个参数为this,意思是把资源文件挂载到上下文ViewGroup
View view = LayoutInflater.from(context).inflate(R.layout.activity_customview_item, this);
//找出自定义xml文件中的id
leftImage = (ImageView) view.findViewById(R.id.iv_left);
myText = (TextView) view.findViewById(R.id.tv_mytext);
rightImage = (ImageView) findViewById(R.id.iv_right);
//TypedArray是一个用来存放由context.obtainStyledAttributes获得的自定义属性的数组
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
//属性的名称是styleable中的名称+“_”+属性名称
Drawable leftDraw = typedArray.getDrawable(R.styleable.MyView_leftImage);
String myString = typedArray.getString(R.styleable.MyView_myText);
Drawable rightDraw = typedArray.getDrawable(R.styleable.MyView_rightImage);
leftImage.setImageDrawable(leftDraw);
myText.setText(myString);
rightImage.setImageDrawable(rightDraw);
//一定要调用,否则这次的设定会对下次的使用造成影响
typedArray.recycle();
}
}
这样就完成了一个自定义View的构造,可以把它当成一个组件来使用,接下来用这个去实现自己的视图
布局如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="更多"
android:textSize="30dp" />
</RelativeLayout>
<demo.jim.com.myview.CustomView
android:id="@+id/myitem01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
my:leftImage="@drawable/leftimage01"
my:myText="联系客服"
my:rightImage="@drawable/rightimage">
</demo.jim.com.myview.CustomView>
<demo.jim.com.myview.CustomView
android:id="@+id/myitem02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
my:leftImage="@drawable/leftimage02"
my:myText="常见问题"
my:rightImage="@drawable/rightimage">
</demo.jim.com.myview.CustomView>
<demo.jim.com.myview.CustomView
android:id="@+id/myitem03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
my:leftImage="@drawable/leftimage03"
my:myText="服务范围"
my:rightImage="@drawable/rightimage">
</demo.jim.com.myview.CustomView>
<demo.jim.com.myview.CustomView
android:id="@+id/myitem04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
my:leftImage="@drawable/leftimage04"
my:myText="关于我们"
my:rightImage="@drawable/rightimage">
</demo.jim.com.myview.CustomView>
<demo.jim.com.myview.CustomView
android:id="@+id/myitem05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
my:leftImage="@drawable/leftimage05"
my:myText="意见协议"
my:rightImage="@drawable/rightimage">
</demo.jim.com.myview.CustomView>
<demo.jim.com.myview.CustomView
android:id="@+id/myitem06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
my:leftImage="@drawable/leftimage06"
my:myText="用户反馈"
my:rightImage="@drawable/rightimage">
</demo.jim.com.myview.CustomView>
</LinearLayout>
这句是自定义命名空间在android studio中的写法
xmlns:my="http://schemas.android.com/apk/res-auto"
最后新建一个activity把上图的layout文件显示出来即可,效果如下
源码地址:
http://download.csdn.net/detail/hzmhzm123/8938063