[英]How to set Layoutparams height/width in dp value?
I tried setting height/width manually in button but it didn't work.我尝试在按钮中手动设置高度/宽度,但没有用。 Then implemented Layoutparams.
然后实现了Layoutparams。 But size shows small and not getting required dp value.
但是 size 显示很小并且没有获得所需的 dp 值。
XML 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.当您在
LayoutParams
中以编程方式指定值时,这些值应为像素。
To convert between pixels and dp you have to multiply by the current density factor.要在像素和 dp 之间进行转换,您必须乘以当前的密度因子。 That value is in the
DisplayMetrics
, that you can access from a Context
:该值位于
DisplayMetrics
,您可以从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);
.
.
ViewGroup.LayoutParams params = ListView.getLayoutParams();
params.height = (int) (50 * customsDebts.size() * (getResources().getDisplayMetrics().density));
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
ListView.setLayoutParams(params);
I believe you should be using a dp value defined in dimens along with getDimensionPixelSize .我相信您应该使用在维度中定义的dp 值以及getDimensionPixelSize 。 In a custom view, the Kotlin implementation would look like:
在自定义视图中,Kotlin 实现如下所示:
val layoutParams = layoutParams
val width = context.resources.getDimensionPixelSize(R.dimen.width_in_dp)
layoutParams.width = width
Option 1: Use dimens.xml选项 1:使用 dimens.xml
view.updateLayoutParams {
width = resources.getDimensionPixelSize(R.dimen.my_width)
height = resources.getDimensionPixelSize(R.dimen.my_height)
}
Option 2: Dispense with dimens.xml选项 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.