如何以编程方式创建一个带有内容的布局(比如里面的组件?)

时间:2016-10-07 21:56:50

标签: java android layout

以下是我已经尝试过的事情。我发现了错误。

void createAndFillContent(Context context, AttributeSet attributeSet){
    this.setPadding(22, 0, 22, 0);

    CustomTextView title = new CustomTextView(context, attributeSet);
    ImageView icon = new ImageView(context, attributeSet);
    ImageView icon_next = new ImageView(context, attributeSet);


    /**
     * setting id's
     */
    icon.setId(R.id.icon_id);
    icon_next.setId(R.id.icon_next_id);
    title.setId(R.id.title_id);

    /**
     * working with title component
      */
    title.setText(this.title);
    title.setTextSize(30);
    title.setFont(LATO_REGULAR);
    title.setNextFocusRightId(1);

    LayoutParams params = (LayoutParams) title.getLayoutParams();
    params.width = LayoutParams.WRAP_CONTENT;
    params.height = LayoutParams.WRAP_CONTENT;
    params.setMarginStart(20);
    params.addRule(CENTER_VERTICAL);
    params.addRule(RelativeLayout.ALIGN_PARENT_START);
    title.setLayoutParams(params);

    /**
     * working with icon
     */
    icon.setImageResource(R.drawable.ic_menu_icon_1);

    params = (LayoutParams) icon.getLayoutParams();
    params.width = LayoutParams.WRAP_CONTENT;
    params.height = LayoutParams.WRAP_CONTENT;
    params.addRule(CENTER_VERTICAL);
    icon.setLayoutParams(params);

    /**
     * working with icon_next
     */

    icon_next.setImageResource(R.drawable.ico_next);

    params = (LayoutParams) icon_next.getLayoutParams();
    params.width = 10;
    params.height = 20;
    params.addRule(CENTER_VERTICAL);
    params.addRule(RelativeLayout.ALIGN_PARENT_END);
    icon_next.setLayoutParams(params);

    /**
     * adding everything to a layout
     */

    this.addView(title);
    this.addView(icon);
    this.addView(icon_next);
}

这是一个错误代码:

N: main Process: com.example.arsen.movietrip.intellsol.com.movietrip.intellsol.com.movietrip.intellsol.com.movietrip, PID: 23601
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arsen.movietrip.intellsol.com.movietrip.intellsol.com.movietrip.intellsol.com.movietrip/movietrip.intellsol.com.movietrip.MainActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class movietrip.intellsol.com.movietrip.component.MenuItem
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)

0 个答案:

没有答案