设置EditText的宽度

发布于 2024-11-06 04:53:47 字数 103 浏览 0 评论 0原文

我在 XML 文件中将 EditText 元素的宽度设置为 fill_parent,但从代码访问它时,它返回 0。 我想要的是运行时该元素的实际宽度。我怎么能做到呢。

I am setting the width of EditText element to fill_parent in XML file, but when accessing it from code, it returns 0.
What I want, is the actual width of that element at run time. How could I do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2024-11-13 04:53:47

使用 getMeasuredWidth():http://developer.android。 com/reference/android/view/View.html#getMeasuredWidth%28%29

另请参阅此处:

当View的measure()方法返回时,必须设置它的getMeasuredWidth()和getMeasuredHeight()值,以及该View的所有后代的值。 View 的测量宽度和测量高度值必须遵守 View 父级施加的约束。这保证了在测量结束时,所有家长都会接受孩子的所有测量结果。父视图可以对其子视图多次调用measure()。例如,父级可以使用未指定的尺寸对每个子级进行一次测量,以了解它们想要多大,然后如果所有子级不受约束的尺寸之和太大或太小,则使用实际数字再次对它们调用measure()(即,如果孩子们对于各自获得多少空间不一致,家长将进行干预并在第二次通过时制定规则)。

http://developer.android.com/guide/topics/ui /how-android-draws.html

Use getMeasuredWidth(): http://developer.android.com/reference/android/view/View.html#getMeasuredWidth%28%29

Read also here:

When a View's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View's descendants. A View's measured width and measured height values must respect the constraints imposed by the View's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent View may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (i.e., if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass).

http://developer.android.com/guide/topics/ui/how-android-draws.html

小霸王臭丫头 2024-11-13 04:53:47

您可以通过创建自定义视图并重写 onMeasure() 方法来解决此问题。如果您始终使用“fill_parent”作为 xml 中的layout_width,则传递到 onMeasusre() 方法的 widthMeasureSpec 参数应包含父级的宽度。

public class MyCustomView extends EditText {

public MyCustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
}
}   

您的 XML 看起来像这样:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <view
        class="com.company.MyCustomView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

You could solve this by creating a custom View and override the onMeasure() method. If you always use "fill_parent" for the layout_width in your xml then the widthMeasureSpec parameter that is passed into the onMeasusre() method should contain the width of the parent.

public class MyCustomView extends EditText {

public MyCustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
}
}   

Your XML would look something like this:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <view
        class="com.company.MyCustomView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>
假装不在乎 2024-11-13 04:53:47

我将向您推荐有关该主题的先前答案,该答案应该会有所帮助:

如何检索视图的尺寸?

I'll refer you to a previous answer on this topic that should be helpful:

How to retrieve the dimensions of a view?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文