美文网首页
通过 程序 设置 layout 参数

通过 程序 设置 layout 参数

作者: zhengzhoufeng | 来源:发表于2017-10-22 08:25 被阅读0次

    转自: android编程中setLayoutParams方法设置

    第一篇

    private LinearLayout generateHeadOfControl() {

    LinearLayout LayoutHead = createLayout(LinearLayout.HORIZONTAL);

    Button DateButton = generateDateButton();

    Button ItemButton = generateItemButton();

    DateButton.setLayoutParams(new LinearLayout.LayoutParams(100,

    LinearLayout.LayoutParams.WRAP_CONTENT));

    ItemButton .setLayoutParams(new LinearLayout.LayoutParams(100,

    LinearLayout.LayoutParams.WRAP_CONTENT));

    layoutHead.addView(DateButton);

    layoutHead.addView(ItemButton);

    return layoutHead;

    }

    ==========================

    注意在LinearLayout里设置params用的是ViewGroup的LayoutParams,会出错,上面的代码OK了~

    第二篇

    textView.setLayoutParams(new TextSwitcher.LayoutParams(

    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    为什么要用TextSwitcher的LayoutParams呢。查一查API,可以看到这么一句话These supply parameters to the parent of this view specifying how it should be arranged。也就是说一定要用父控件的LayoutParams。如果父控件是LinearLayout,当然就必须写成LinearLayout.LayoutParams

    http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

    LayoutParams are used by views to tell their parents how they want to be laid out. SeeViewGroup Layout Attributesfor a list of all child view attributes that this class supports.

    The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:

    FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)

    WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)

    an exact number

    There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams which adds an X and Y value.

    Developer Guides

    For more information about creating user interface layouts, read theXML Layoutsdeveloper guide.

    我在这里看了一篇别人博客对于LayoutParams的解释,我觉的很到位,所以就继续拿来主义。

    其实这个LayoutParams类是用于child view(子视图) 向 parent view(父视图)传达自己的意愿的一个东西(孩子想变成什么样向其父亲说明)其实子视图父视图可以简单理解成

    一个LinearLayout 和 这个LinearLayout里边一个 TextView 的关系 TextView 就算LinearLayout的子视图 child view 。需要注意的是LayoutParams只是ViewGroup的一个内部类这里边这个也就是ViewGroup里边这个LayoutParams类是 base class 基类实际上每个不同的ViewGroup都有自己的LayoutParams子类

    相关文章

      网友评论

          本文标题:通过 程序 设置 layout 参数

          本文链接:https://www.haomeiwen.com/subject/suqguxtx.html