在代码中将自定义视图添加到RelativeLayout:LayoutParams被忽略/自定义视图位置不正确



我面临以下情况:

  • 我有一个自定义布局的DialogFragment,它将包含几个动态插入的视图
  • 这些视图大多是自定义视图

DialogFragment的自定义布局的父容器是RelativeLayout

TL;DR

  • 我有一个自定义布局的DialogFragment
  • 使用addView(customView, params)添加的自定义视图,其中params已使用params.addRule(RelativeLayout.BELOW, lastComponent.getId());定义
  • 它们不位于lastComponent之下。此外,在为核心TextView s工作时,任何其他规则都将被忽略

Popup.onCreateView()

  • 对话框的自定义布局已膨胀
  • PopupComponents(需要添加到对话框的自定义视图)添加到对话框视图
  • 已设置这些组件的ID
  • 分量n用(RelativeLayout.BELOW, [n-1].getId()) 得到RelativeLayout.LayoutParams

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.popup_stickerfound, container, false); // is a RelativeLayout
        RelativeLayout vg = (RelativeLayout) rootView;
        // ...
        // Save the last component to align the next one below it
        View lastComponent = null;
        int idIndex = 1;
        // Render title if it is set
        if(title != null) {
            TextView tvTitle = new TextView(getActivity().getBaseContext());
            tvTitle.setText(title);
            // Set an ID for layouting
            int id = idIndex++;
            if(Build.VERSION.SDK_INT >= 17) {
                id = View.generateViewId();
            }
            tvTitle.setId(id);
            lastComponent = tvTitle;
            // Add title to our layout
            vg.addView(tvTitle, titleParams);
        }
        // Get the component (shortened for simplicity, iterator is set and all working)
        PopupComponent pc = iterator.next();
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        if(lastComponent != null) {
            params.addRule(RelativeLayout.BELOW, lastComponent.getId());
        }         
        // Give the view a dynamically created ID for layouting reasons
        int id = idIndex++;
        if(Build.VERSION.SDK_INT >= 17) {
            id = View.generateViewId();
        }
        if(pc.getId() == 0) {
            pc.setId(id);
        }
        // Attach the component to the popup and apply the layout params.
        pc.setLayoutParams(params);
        vg.addView(pc, params);
        lastComponent = pc;
        i++;      
    

vg.addView(pc, params)不起作用。下面显示了结果,但与上面的代码不匹配(添加了一个额外的TextView——我稍后会讨论):[屏幕截图]


我尝试了另一种解决方案:膨胀布局并定义根视图:

PopupComponent.attachTo(上下文上下文,ViewGroup容器)

public View attachTo(Context context, ViewGroup container) {
    //View v =  View.inflate(context, layoutRes,
      //      container);
    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = li.inflate(layoutRes, container, false);
    insertData();
    return v;
}

Popup.onCreateView()

// see the end of the snippet above 
// Attach the component to the popup and apply the layout params.
pc.attachTo(getActivity().getBaseContext(), vg);

但这也失败了,给了我一个空洞的结果:[屏幕截图2]<因为我只有不到10个名声而被排除在外,哇。可以添加到评论中吗?


如果我在li.inflate(layoutRes, container, false)->li.inflate(layoutRes, container, true)中将attachToRoot设置为true,则自定义组件是可见的,但LayoutParams似乎被忽略了:[屏幕截图3]

奇怪的是,它一开始适用于TextView(s)(我在代码中只描述了一个,实际上有两个用于测试目的)。那么,我的自定义View不应该像TextView一样处理吗?它与视图"没有太大区别"?

知道这里出了什么问题吗?

U可以使用LinearLayout而不是RelativeLayout来添加视图。并将方向设置为LinearLayout。

最新更新