【问题标题】:how to add a horizontal divider programmatically如何以编程方式添加水平分隔线
【发布时间】:2016-12-26 15:49:50
【问题描述】:

在 bleow 发布的代码中,我试图在每个添加到线性布局的视图之后添加一个水平分隔线,如代码所示。 我遇到的问题是,在运行时没有显示分隔符

请告诉我为什么没有显示分隔线以及如何让它显示?

代码

private void inflateView(String bez, String ges) {
    LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungsListeActivity2mod_linLay_meineDocList_container);

    //divider
    View viewDivider = new View(this);
    viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    viewDivider.setBackgroundColor(Color.parseColor("#000000"));

    LayoutInflater inflator = this.getLayoutInflater();
    View view = inflator.inflate(R.layout.versicherung_docs_row_model, null);//<<<<<< this is the view i want to add to the map as a key

    ImageView imgViewLogo = (ImageView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_imgVie_logo);
    TextView texVieBez = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docBezeichnung);
    TextView texVieExtraItem = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_addMoreDocs);
    TextView texVieGes = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docGesellschaft);
    Button btnMore = (Button) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_btn_more);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc, this.getTheme()));
    } else {
        imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc));
    }

    texVieBez.setText(bez);
    texVieGes.setText(bez);
    btnMore.setVisibility(View.VISIBLE);

    linLay.addView(view);

    linLay.addView(viewDivider);
}

【问题讨论】:

    标签: android view android-linearlayout android-relativelayout


    【解决方案1】:

    viewDivider 的高度为 WRAP_CONTENT,由于视图为空,其高度计算为 0。

    您必须设置所需的分隔线高度。

    int dividerHeight = getResources().getDisplayMetrics().density * 1; // 1dp to pixels
    viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dividerHeight));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2011-08-05
      相关资源
      最近更新 更多