您的位置:首页 > 产品设计 > UI/UE

Activity里面的UI模板自定义属性,更好的复用TextView,由于在网上没找到类似,记录下来。

2015-08-11 20:09 435 查看
首先是一个模板     

然后出来效果,其实后面姓名还有一个框  都是一起的, 你用的时候只需要引用一个,就可以达到你自己用2个textview和一个image。

开始是写了蛮多的Textview,然后自己仔细的想一下复用,抽取共通的一些代码,想了一下他的实现方式,应该和Adapter是类似的。

首先是模板的定义,我首先定义的是一个框和图标,其实后面也都有一些姓名什么的。

<RelativeLayout
android:id="@+id/activity_my_data_username_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
>
<TextView
android:id="@+id/activity_my_data_user"
style= "@style/my_data_item_style"
/>
<TextView
android:id="@+id/activity_my_data_name"
style= "@style/my_data_item_style"
android:layout_toLeftOf="@+id/activity_my_data_username_arraw" />
<ImageView
android:id="@+id/activity_my_data_username_arraw"
style= "@style/my_data_item_button" />
</RelativeLayout>
<include
layout= "@layout/include_separated_line"
/>

其次就是自定义组件了。
首先自定义组件这边的话,定义属性,然后在属性里面就方便定义textview。(我写的比较简单。大神勿喷。)

public MyDataitemLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.control_my_datas_item,this,true);

activity_my_data_user = (TextView)findViewById(R.id.activity_my_data_user);
activity_my_data_name = (TextView)findViewById(R.id.activity_my_data_name);

int resouceId = attrs.getAttributeResourceValue(null,"Text", 0);
if (resouceId != 0) {
activity_my_data_user.setText(resouceId);
}

int value = attrs.getAttributeResourceValue(null, "value",0);
if (value != 0) {
activity_my_data_name.setText(value);
}
}

最后只需要在XML里面引用

<cn.sharing8.blood.control.MyDataitemLayout
android:id="@+id/activity_my_data_username_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Text="@string/user_username"
value=""/>

<include layout="@layout/include_separated_line" />

<cn.sharing8.blood.control.MyDataitemLayout
android:id="@+id/activity_my_data_sex_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Text="@string/user_sex"
value=""/>

<include layout= "@layout/include_separated_line"/>

<cn.sharing8.blood.control.MyDataitemLayout
android:id="@+id/activity_my_data_phone_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Text="@string/user_phone"
value=""/>

<include layout= "@layout/include_separated_line"/>

<cn.sharing8.blood.control.MyDataitemLayout
android:id="@+id/activity_my_data_mail_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Text="@string/user_mail"
value=""/>

<include layout= "@layout/include_separated_line"/>

<cn.sharing8.blood.control.MyDataitemLayout
android:id="@+id/activity_my_data_idcard_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Text="@string/user_idcard"
value=""/>

<include layout= "@layout/include_separated_line" />

<cn.sharing8.blood.control.MyDataitemLayout
android:id="@+id/activity_my_data_address_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Text="@string/user_address"
value=""/>有木有发现少了很多代码。   比你一个一个写省事多了。
    好吧。。其实我就是方便自己偷懒吧。。。啊哈哈。。自己研究出来也蛮开心的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: