【发布时间】:2018-07-11 17:54:11
【问题描述】:
我已经成功地制作了一个自定义 PinView 类。 现在的问题是我想根据 XML 中指定的 PinView 大小来设置 pin 视图按钮的大小。就像一个按钮会有 5% 的 PinView 宽度和高度。
目前,我通过这样做来设置 PinView 的高度和宽度,getPinPadHeight() 返回屏幕高度的 80% 和屏幕宽度的 70%
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
pinView.getLayoutParams().height = ViewUtil.getPinPadHeight();
pinView.getLayoutParams().width = ViewUtil.getPinPadWidth();
}
但现在的问题是,当我像这样在我的主要活动 XML 文件中使用此自定义 PinView 视图时。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
tools:context=".MainActivity">
<production.kado.lock.views.PinView
android:id="@+id/pinView"
android:layout_centerInParent="true"
android:layout_width="300dp"
android:layout_height="200dp"
app:changePassword="true" />
</RelativeLayout>
我想改变它的高度和宽度它不改变它总是根据屏幕高度和宽度百分比设置我怎样才能让这个视图像一些库一样我可以根据我的偏好设置高度和宽度以及它里面的视图自动当我将使用此自定义视图时,根据 xml 属性测量自己
public class PinView extends RelativeLayout {
public PinView(Context context) {
super(context);
initView();
}
public PinView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public PinView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
pinView.getLayoutParams().height = ViewUtil.getPinPadHeight();
pinView.getLayoutParams().width = ViewUtil.getPinPadWidth();
}
}
【问题讨论】:
-
@Pierre onMeausre 调用了太多次,每次都给出不同的高度和宽度,你能用一些例子解释一下吗
-
嗯,首先..
ViewUtil.getPinPadHeight()返回什么? -
所以问题是,你必须有办法查看 pinView 是否设置为 match_parent 或具有 dp 宽度/高度。您可以为 pinview 添加另一个名为 customsize 的属性,如果为 true,则表示使用了自定义宽度/高度 dp,如果为 false,则使用您的
ViewUtil.getPinPadHeight/Width() -
@Pierre
public static int getPinPadHeight() { return (int) (AppUtils.getScreenHeight() * 70 / 100); }我正在计算屏幕尺寸的百分比 -
好的,谢谢我明白你的意思,请告诉我应该在
onAttachedToWindow()方法中这样做吗?