I Dim a view by inflate the layout, Code like this:
public class MenuItemView extends RelativeLayout {
private Context context;
TextView tv_title;
String title = "";
TextView tv_content;
String content = "";
TextView tv_guid;
String guid = "";
ImageView iv_divider;
ImageView iv_arrow;
RelativeLayout rl_main;
public MenuItemView(Context context) {
super(context);
initView(context, null);
}
public MenuItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).layout(l, t, r, b);
}
}
private void initView(Context context, AttributeSet attrs) {
this.context = context;
inflate(context, R.layout.menu_item_view_layout, this);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_content = (TextView) findViewById(R.id.tv_content);
tv_guid = (TextView) findViewById(R.id.tv_guid);
iv_arrow = (ImageView) findViewById(R.id.iv_arrow);
iv_divider = (ImageView) findViewById(R.id.iv_divider);
rl_main = (RelativeLayout) findViewById(R.id.rl_main);
//Default
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MenuItemView);
title = a.getString(R.styleable.MenuItemView_menu_title);
content = a.getString(R.styleable.MenuItemView_menu_content);
guid = a.getString(R.styleable.MenuItemView_menu_guid);
a.recycle();
}
//
tv_title.setText(title);
tv_content.setText(content);
tv_guid.setText(guid);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private int getpx(int dp) {
return (int) (context.getResources().getDisplayMetrics().density * dp + 0.5f);
}
}
when I use it in layout.like this
while is performed right, but when I use it in times, like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<setmenu.smzdm.com.menuitemview.MenuItemView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu_content="Content"
app:menu_guid="Guid"
app:menu_title="Title" />
<setmenu.smzdm.com.menuitemview.MenuItemView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu_content="Content"
app:menu_guid="Guid"
app:menu_title="Title" />
<setmenu.smzdm.com.menuitemview.MenuItemView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu_content="Content"
app:menu_guid="Guid"
app:menu_title="Title" />
</LinearLayout>
It can just show one view,like:
when it run on mobile, it is also just can show one view in layout.