【问题标题】:Set height and width of custom view according to xml根据xml设置自定义视图的高度和宽度
【发布时间】: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() 方法中这样做吗?

标签: android view


【解决方案1】:

如何在 onSizeChanged 中进行计算?

【讨论】:

    【解决方案2】:

    Res/values/attrs.xml添加另一个属性

    <declare-styleable name="PinView">
        <attr name="changePassword" format="boolean" />
        <attr name="customSize" format="boolean" />
    </declare-styleable>
    

    在带有属性的构造函数中,检索 customSize 布尔值

    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.PinView, 0, 0);
    //Set CustomSize boolean Global Variable
    customSize = a.getBoolean(R.styleable.PinView_customSize, false);
    

    然后更改您的onAttachedToWindow 方法:

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(!customSize) {
            pinView.getLayoutParams().height = ViewUtil.getPinPadHeight();
            pinView.getLayoutParams().width = ViewUtil.getPinPadWidth();
        }
    }
    

    这样,如果app:customSize="true"那么它将跳过ViewUtil.getPinPadHeight()ViewUtil.getPinPadWidth()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-31
      • 2011-06-29
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      相关资源
      最近更新 更多