【问题标题】:How to set Layoutparams height/width in dp value?如何在 dp 值中设置 Layoutparams 高度/宽度?
【发布时间】:2017-01-15 08:44:22
【问题描述】:

我尝试在按钮中手动设置高度/宽度,但没有成功。然后实现了Layoutparams。但是尺寸显示很小并且没有获得所需的 dp 值。

XML

 <Button
    android:id="@+id/itemButton"
    android:layout_width="88dp"
    android:layout_height="88dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:background="#5e5789"
    android:gravity="bottom"
    android:padding="10dp"
    android:text=""
    android:textColor="#FFF"
    android:textSize="10sp" />

构造函数:

  public Item (int id, String name, String backgroundColor, String textColor, int width, int height){
    this.id = id;
    this.name = name;
    this.backgroundColor = backgroundColor;
    this.textColor = textColor;
    this.width = width;
    this.height = height;

}

适配器:

@Override public void onBindViewHolder(final ViewHolder holder, int position) {
    final Item item = items.get(position);
    holder.itemView.setTag(item);
    holder.itemButton.setText(item.getName());
    holder.itemButton.setTextColor(Color.parseColor(item.getTextColor()));
    holder.itemButton.setBackgroundColor(Color.parseColor(item.getBackgroundColor()));
    ViewGroup.LayoutParams params = holder.itemButton.getLayoutParams();
    params.width = item.getWidth();
    params.height = item.getHeight();
    holder.itemButton.setLayoutParams(params);

}

【问题讨论】:

    标签: android xml android-layout android-adapter


    【解决方案1】:

    当您以编程方式在LayoutParams 中指定值时,这些值应为像素。

    要在像素和 dp 之间进行转换,您必须乘以当前密度因子。该值在DisplayMetrics 中,您可以从Context 访问:

    float pixels =  dp * context.getResources().getDisplayMetrics().density;
    

    所以在你的情况下你可以这样做:

    .
    .
    float factor = holder.itemView.getContext().getResources().getDisplayMetrics().density;
    params.width = (int)(item.getWidth() * factor);
    params.height = (int)(item.getHeight() * factor);
    .
    .
    

    【讨论】:

    • 你的解释很清楚很准确!谢啦!成功了!
    【解决方案2】:

    我相信您应该使用 dimens 中定义的 dp 值 以及 getDimensionPixelSize。在自定义视图中,Kotlin 实现如下所示:

    val layoutParams = layoutParams
    val width = context.resources.getDimensionPixelSize(R.dimen.width_in_dp)
    layoutParams.width = width
    

    【讨论】:

      【解决方案3】:
        ViewGroup.LayoutParams params = ListView.getLayoutParams();
              params.height = (int) (50 * customsDebts.size() * (getResources().getDisplayMetrics().density));
              params.width = ViewGroup.LayoutParams.MATCH_PARENT;
              ListView.setLayoutParams(params);
      

      【讨论】:

        【解决方案4】:

        选项1:使用dimens.xml

        view.updateLayoutParams {
            width = resources.getDimensionPixelSize(R.dimen.my_width)
            height = resources.getDimensionPixelSize(R.dimen.my_height)
        }
        

        选项 2:使用 dimens.xml 删除

        /** Converts dp to pixel. */
        val Int.px get() = (this * Resources.getSystem().displayMetrics.density).toInt()
        
        view.updateLayoutParams {
            width = 100.px
            height = 100.px
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-23
          • 2011-07-12
          • 2015-04-24
          • 1970-01-01
          • 2020-08-29
          • 1970-01-01
          • 1970-01-01
          • 2012-06-16
          相关资源
          最近更新 更多