如何在dp值中设置Layoutparams的高度/宽度? [英] How to set Layoutparams height/width in dp value?

查看:74
本文介绍了如何在dp值中设置Layoutparams的高度/宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

XML

 <按钮android:id="@+id/itemButton"android:layout_width="88dp"android:layout_height="88dp"机器人:layout_marginRight="5dp"机器人:layout_marginBottom="5dp"机器人:背景=#5e5789"机器人:重力=底部"机器人:填充=10dp"机器人:文本="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 = 名称;this.backgroundColor = backgroundColor;this.textColor = textColor;this.width = 宽度;this.height = 高度;}

适配器:

@Override public void onBindViewHolder(final ViewHolder holder, int position) {最终项目 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);}

解决方案

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

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

浮动像素 = dp * context.getResources().getDisplayMetrics().密度;

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

<预><代码>..浮动因子 = holder.itemView.getContext().getResources().getDisplayMetrics().密度;params.width = (int)(item.getWidth() * factor);params.height = (int)(item.getHeight() * factor);..

I tried setting height/width manually in button but it didn't work. Then implemented Layoutparams. But size shows small and not getting required dp value.

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" />

Constructor:

  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;

}

Adapter:

@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);

}

解决方案

When you specify values programmatically in the LayoutParams, those values are expected to be pixels.

To convert between pixels and dp you have to multiply by the current density factor. That value is in the DisplayMetrics, that you can access from a Context:

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

So in your case you could do:

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

这篇关于如何在dp值中设置Layoutparams的高度/宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆