更改自定义按钮尺寸布局和容器 [英] Change custom button size for layouts and containers

查看:177
本文介绍了更改自定义按钮尺寸布局和容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有扩展的TextView类的自定义类(touchbutton)。我无法手动调整按钮。我能得到的按钮只有一个类型的布局或容器的工作,但不能同时使用。在大多数情况下,Touchbutton在GridView的,所以我的方法来改变大小像这样:

I have a custom class (touchbutton) extending the TextView class. I am having trouble resizing the button manually. I can get the button to work for only one type of layout or container, but not both. For the most part, Touchbutton is in gridviews so my method to change the size is as so:

private void setLayout(buttonsize_t size) {

    log("Setting Layout: "+buttonsize_t.getPxl(size));

    final float scale = getContext().getResources().getDisplayMetrics().density;
    int dim = (int) (buttonsize_t.getPxl(size) * scale + 0.5f);

    AbsListView.LayoutParams params = (AbsListView.LayoutParams) getLayoutParams();
    if (params != null) {
        params.height = dim;
        params.width = dim;
    }
    else {
        params = new AbsListView.LayoutParams(dim,dim);
    }
    setLayoutParams(params);
}

然而,当TouchButton中的LinearLayout调整大小(例如)我得到与logcat的崩溃:

However, when the TouchButton is resized in a LinearLayout (for example) I get a crash with the Logcat:

09-01 19:18:35.630: ERROR/AndroidRuntime(20793): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
09-01 19:18:35.630: ERROR/AndroidRuntime(20793):     at com.ians.aac3.TouchButton.setLayout(TouchButton.java:204)

204线是指则params的实例。

Line 204 refers to the instantiation of params.

我注意到,无论 GridView控件的LinearLayout 共享父的ViewGroup ,所以我尝试使用 ViewGroup.LayoutParams 。此,但是,这将导致对GridView的相同的行为(logcat中引述同一行中)。

I noticed that both GridView and LinearLayout share the parent ViewGroup, so i tried using ViewGroup.LayoutParams. This, however, this will lead to the same behavior for gridviews (logcat citing the same line).

有谁知道我怎么会做这项工作对任何类型的布局或小工具?

Does anyone know how I might make this work for any type of layout or widget?

更新:
由于推荐我用视图组再次尝试:

UPDATE: As recommended i tried with View group again:

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
.....
ViewGroup.LayoutParams params = getLayoutParams();
        if (params != null) {
            params.height = dim;
            params.width = dim;
        }
        else {
            params = new LayoutParams(dim,dim);
        }
        setLayoutParams(params);

或没有试图回收当前的LayoutParams:

or without trying to recycle the current layoutparams:

setLayoutParams(new ViewGroup.LayoutParams(dim, dim));

和我得到同样的错误类型:

And i get the same type of error:

09-02 09:10:49.680: ERROR/AndroidRuntime(7069): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.widget.GridView.onMeasure(GridView.java:1028)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.View.measure(View.java:10828)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.View.measure(View.java:10828)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-02 09:10:49.680: ERROR/AndroidRuntime(7069):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)

更新2:
看来使用上面指定的ViewGroup.LayoutParams作品为的LinearLayout。然而,正如上面所看到的,在GridView不喜欢它... ...

Update 2: It seems that using the ViewGroup.LayoutParams specified above works for the LinearLayout. However, as seen above, the gridview doesn't like it...

推荐答案

要了解这里发生了什么,你需要在Android源看。有一个在ViewGroup中的方法 generateLayoutParams 的Javadoc的状态:

To understand what happens here you need to look in the Android source. There is a method in ViewGroup generateLayoutParams which JavaDoc states:

,则返回一个安全组的基础上所提供的布局的布局参数
  PARAMS。当一个ViewGroup中传递一个查​​看其布局PARAMS不
  通过考验
  <一href=\"http://developer.android.com/reference/android/view/ViewGroup.html#checkLayoutParams%28android.view.ViewGroup.LayoutParams%29\"相对=nofollow> checkLayoutParams(android.view.ViewGroup.LayoutParams) ,这种方法是
  调用。这个方法应该返回一组新的布局适合PARAMS
  这个ViewGroup中,可能是通过复制适当的属性
  从指定的一组布局则params的。

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

如果你看的LinearLayout和AbsListView(GridView控件的母公司)的源你会看到它们转换成自己的孩子布置PARAMS到 LinearsLayout.LayoutParams AbsListView.LayoutParams 分别。但是,当儿童被添加到布局此转换才会发生。结果,
因此,如果您添加TouchButton到的LinearLayout(programmaticaly或通过XML),将获得 LinearsLayout.LayoutParams ,如果你(通过转接器)将其添加到GridView接收 AbsListView.LayoutParams 。结果
但是,如果你设置布局PARAMS手动之后,您将获得 ClassCastException异常某处父容器code(因为它预计其子女布局PARAMS是某种特定类型)

If you look at LinearLayout and AbsListView (the parent of GridView) source you'll see they convert their children layout params to LinearsLayout.LayoutParams and AbsListView.LayoutParams respectively. But this conversion only happens when the child is added to the layout.
Thus if you add your TouchButton to the LinearLayout (programmaticaly or via XML) it will receive LinearsLayout.LayoutParams and, if you add it to the GridView (via an adapter) it receives AbsListView.LayoutParams.
But if you set layout params manually afterwards, you will get ClassCastException somewhere in the parent container code (since it expects its children layout params to be of some specific type).

有关您的问题的解决,我建议你如下:

For the solution of your issue I suggest you the following:

private void setLayout(buttonsize_t size) {

    log("Setting Layout: "+buttonsize_t.getPxl(size));

    final float scale = getContext().getResources().getDisplayMetrics().density;
    int dim = (int) (buttonsize_t.getPxl(size) * scale + 0.5f);

    ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) getLayoutParams();
    if (params != null) {  
        params.height = dim;
        params.width = dim;
        setLayoutParams(params);
    } else {
      // the LayoutParams was not set yet, it must be the GridView 
      // which delays setting till child rendering
      params = new AbsListView.LayoutParams(dim, dim);
      setLayoutParams(params);
    }
}

这篇关于更改自定义按钮尺寸布局和容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆