recyclerview设置layoutparams中的项目错误

时间:2015-08-21 12:30:57

标签: android android-layout android-recyclerview android-adapter

我想在我的通知应用中: 标题存在,文本不存在=标题应垂直居中 文本存在,标题不存在=文本应垂直居中

我的适配器看起来像这样:

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();
        params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

        holder.view_title.setLayoutParams(params);

我发现我可以在我的代码中设置“android:layout_centerVertical =”true“:

getElementsByClassName()

一切正常。 但有时会注意到标题或文字的中心,尽管标题和文字都存在。怎么样?

看起来像这样: enter image description here

抱歉我的英语不好:D 有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您忘记删除两个文本的最后var frisby = require('frisby'); describe("setUp and tearDown", function(){ beforeAll(function(){ console.log("test beforeAll"); }); afterAll(function(){ console.log("afterAll"); }); //FRISBY TESTS }); //end of describe function 块中的CenterVertical属性。并且不要忘记将文本对齐,以免相互叠加。

else

要删除规则,请使用:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();

RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) holder.view_text.getLayoutParams();

    if ( ( holder.title.getText().length() > 0) && (holder.text.getText().length() == 0) ) {

            holder.view_title.setVisibility(View.VISIBLE);
            holder.view_text.setVisibility(View.INVISIBLE);

            params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

            holder.view_title.setLayoutParams(params);

            debug("title: " + holder.title.getText());
            debug("text: " + holder.text.getText());

        } else if ( ( holder.text.getText().length() > 0) && (holder.title.getText().length() == 0) ) {

            holder.view_title.setVisibility(View.INVISIBLE);
            holder.view_text.setVisibility(View.VISIBLE);

            params2.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            holder.view_text.setLayoutParams(params2);
        } else {
            // YOU NEED TO RESET YOUR 2 LAYOUT PARAMS HERE
            params.addRule(RelativeLayout.CENTER_VERTICAL, 0);
            params2.addRule(RelativeLayout.CENTER_VERTICAL, 0);

            // ..... 
            // align your texts here
            // ....

            holder.view_title.setLayoutParams(params);
            holder.view_text.setLayoutParams(params2);

            holder.view_title.setVisibility(View.VISIBLE);
            holder.view_text.setVisibility(View.VISIBLE);
        }

或(对于API级别> = 17

layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, 0);

答案 1 :(得分:0)

代码,

 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) submitCashbackFailedViewHolder.contentViewRlly.getLayoutParams();

    layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;

    layoutParams.width = UiUtil.dpToPx(mContext,UiUtil.px2dip(UiUtil.getScreenWidth()) - 23);

问题在于

layoutParams.height需要px​​。
 但我用dp来设置它