
效果图
1、自定义一个布局
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"
androID:orIEntation="vertical">
<ImageVIEw
androID:ID="@+ID/account_icon"
androID:layout_wIDth="30dp"
androID:layout_height="30dp"
androID:layout_centerVertical="true"
androID:layout_margin="10dp"
androID:src="@drawable/wechat" />
<TextVIEw
androID:ID="@+ID/account_Title"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_centerVertical="true"
androID:layout_toRightOf="@+ID/account_icon"
androID:text="姓名"
androID:textcolor="@color/colorBlack" />
<ImageVIEw
androID:ID="@+ID/account_rightArrow"
androID:layout_wIDth="20dp"
androID:layout_height="20dp"
androID:layout_alignParentRight="true"
androID:layout_centerVertical="true"
androID:layout_marginRight="10dp"
androID:src="@drawable/right"
androID:visibility="visible" />
<TextVIEw
androID:ID="@+ID/account_text"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_centerInParent="true"
androID:layout_toleftOf="@+ID/account_rightArrow"
androID:text="张三"
androID:visibility="visible" />
<TextVIEw
androID:ID="@+ID/account_bottom"
androID:layout_wIDth="match_parent"
androID:layout_height="1dp"
androID:layout_alignParentBottom="true"
androID:background="@color/colorGray" />
</relativeLayout>
2、在res资源目录下的values 目录下新建一个attres 文件
<?xml version="1.0" enCoding="utf-8"?>
<resources>
<declare-styleable name="item_vIEw">
<attr name="left_icon" format="integer" /><!--左侧图标-->
<attr name="left_Title" format="string" /><!--左侧标题文字-->
<attr name="right_text" format="string" /><!--右侧描述文字-->
<attr name="show_right_text" format="boolean" /><!--是否显示右侧描述文字-->
<attr name="show_right_arrow" format="boolean" /> <!--是否显示右侧箭头-->
<attr name="show_bottomline" format="boolean" /> <!--是否显示下划线-->
</declare-styleable>
</resources>
3、新建一个类 AccountSetUp 继承 布局 如(linearLayout)
public class AccountSetUp extends linearLayout {
private Boolean isBottom;
private Boolean isRightText;
private Boolean isRightArrow;
private ImageVIEw leftIcon;
private ImageVIEw rightArrow;
private TextVIEw leftTitle;
private TextVIEw rightText;
private TextVIEw bottomVIEw;
public AccountSetUp(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(getContext()).inflate(R.layout.account_item, this);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.item_vIEw);//解析布局
isBottom = ta.getBoolean(R.styleable.item_vIEw_show_bottomline, true); //底部分割线是否显示
isRightText = ta.getBoolean(R.styleable.item_vIEw_show_right_text, true);//右边描述文字是否显示
isRightArrow = ta.getBoolean(R.styleable.item_vIEw_show_right_arrow, true);//右边箭头是否显示
leftIcon = findVIEwByID(R.ID.account_icon); //左边图标
rightArrow = findVIEwByID(R.ID.account_rightArrow);//右边箭头
leftTitle = findVIEwByID(R.ID.account_Title); //左边标题文字
rightText = findVIEwByID(R.ID.account_text);//左边描述文字
bottomVIEw = findVIEwByID(R.ID.account_bottom); //底部分割线
leftIcon.setimageDrawable(ta.getDrawable(R.styleable.item_vIEw_left_icon));//赋值
rightArrow.setVisibility(isRightArrow ? VISIBLE : GONE);
leftTitle.setText(ta.getText(R.styleable.item_vIEw_left_Title));
rightText.setText(ta.getText(R.styleable.item_vIEw_right_text));
rightText.setVisibility(isRightText ? VIEw.VISIBLE : INVISIBLE);
bottomVIEw.setVisibility(isBottom ? VIEw.VISIBLE : VIEw.INVISIBLE);
//回收属性对象
ta.recycle();
}
/**
* 设置标题内容
*/
public voID setTitle(String TitleText) {
rightText.setText(TitleText);
}
}
4、在xml中使用
5、设置点击事件
逻辑代码:
初始化:
private AccountSetUp accountCollection;
private AccountSetUp accountHistory;
private AccountSetUp accountCache;
private AccountSetUp accountAbout;
/**
* 设置点击
*/
private voID initItem() {
accountCollection = vIEw.findVIEwByID(R.ID.account_vIEw_collection);//我的收藏
// accountCollection.setTitle("");
accountCollection.setonClickListener(this);
accountHistory = vIEw.findVIEwByID(R.ID.account_vIEw_history); //我的历史
accountHistory.setonClickListener(this);
accountCache = vIEw.findVIEwByID(R.ID.account_vIEw_cache); //离线下载
accountCache.setonClickListener(this);
accountAbout = vIEw.findVIEwByID(R.ID.account_vIEw_about); //关于
accountAbout.setonClickListener(this);
}
/**
* 全部点击事件
*/
@OverrIDe
public voID onClick(VIEw vIEw) {
switch (vIEw.getID()) {
case R.ID.account_vIEw_collection:
Intent intentCollection = new Intent(getContext(), CollectionActivity.class);
startActivity(intentCollection);
break;
case R.ID.account_vIEw_history:
Intent intentHistory = new Intent(getContext(), HistoryActivity.class);
startActivity(intentHistory);
break;
case R.ID.account_vIEw_cache:
Intent intentCache = new Intent(getContext(), CollectionActivity.class);
startActivity(intentCache);
break;
case R.ID.account_vIEw_about:
Intent intentAbout = new Intent(getContext(), CollectionActivity.class);
startActivity(intentAbout);
break;
default:
break;
}
}
文章:https://blog.csdn.net/asfang/article/details/79144127
https://blog.csdn.net/q15037911903/article/details/82773279
总结
以上是内存溢出为你收集整理的Android 自定义设置布局全部内容,希望文章能够帮你解决Android 自定义设置布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)