【发布时间】:2018-03-26 06:30:03
【问题描述】:
我是一个 Java 菜鸟,并试图找到以下问题的答案,但无法识别以下问题的正确接口。任何建议将不胜感激。
我在 Android Studio 中有以下可重用的代码:
private RelativeLayout.LayoutParams layoutParamsGenerator() {
//convert pixels to dpi
int pixelHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
int pixelWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
// Sets LayoutParams
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(pixelHeight, pixelWidth);
//ensure placed button is placed in centre
int xButton = addTaskButton.getLeft();
int yButton = addTaskButton.getTop();
//defines the LayoutParameters we will a sssign to the button we created above
layoutParams.leftMargin = xButton;
layoutParams.topMargin = yButton;
return layoutParams;
}
代码读取“addTaskButton”的布局参数并返回。
我现在想让这个方法适用于任何视图对象,而不仅仅是上面代码中当前硬编码的“addTaskButton”(即图像视图、文本视图等)。我知道这需要按照以下Java: Passing multiple parameters to a method 实现接口,但我似乎无法正确实现。
任何建议将不胜感激。
Z
【问题讨论】:
标签: java android interface abstract-class