您的位置:首页 > 其它

创建自定义控件

2015-11-29 14:26 260 查看
1.在layout目录下创建title.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- 自建伪标题ActionBar -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_green_dark">

<ImageView
android:id="@+id/img_title"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="7dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"/>

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/img_title"
android:text="@string/title_tv"/>

<Button
android:id="@+id/btn_title"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp"
android:text="@string/title_settings"/>

</RelativeLayout>


2.新建ownui包创建titleView.java:

/**
* 创建自定义控件
*/
public class TitleLayout extends Layout
{

public TitleLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);//TODO 填充xml

Button btnTitle = (Button) findViewById(R.id.btn_title);

if(getContext() instanceof MainActivity)//判断一个对象是否是指定类型的实例对象
{
btnTitle.setText(R.string.title_settings);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: