1

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

enter image description here

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:

enter image description here

when it run on mobile, it is also just can show one view in layout.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
YunxiuWang
  • 101
  • 1
  • 2

1 Answers1

0

Your view you inflate is not added to your customer view. You should fix:

View view = inflate(context, R.layout.menu_item_view_layout, this);
tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_content = (TextView) view.findViewById(R.id.tv_content);
tv_guid = (TextView) view.findViewById(R.id.tv_guid);
iv_arrow = (ImageView) view.findViewById(R.id.iv_arrow);
iv_divider = (ImageView) view.findViewById(R.id.iv_divider);
rl_main = (RelativeLayout) view.findViewById(R.id.rl_main);
// your code
// and last, you have to add view to customeView
addView(view);
Nha Phạm Thị
  • 409
  • 4
  • 10
  • In mine opinion when “inflate(context, R.layout.menu_item_view_layout, this);”,the view canbe automatelly added to the customview; I get a crash after add"addView(view);" – YunxiuWang May 31 '18 at 02:06
  • android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7189) android.view.View.resetRtlProperties(View.java:17023) android.view.ViewGroup.addViewInner(ViewGroup.java:4967) at android.view.ViewGroup.addView(ViewGroup.java:4750) – YunxiuWang May 31 '18 at 02:10