我想以编程方式制作这条垂直线
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
这就是我创建 View 的方式:
View view = new View(getActivity());
如何添加宽度、高度和背景参数?
最佳答案
试试这个
View v = new View(activityContext);
v.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT), GetPixel(1, activityContext));
v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
和
// To convert pixels to dp units
public int GetPixel(float f , Context context)
{
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int)((f * displayMetrics.density) + 0.5f);
}
关于java - 如何以编程方式添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18845861/