在代码中向RelativeLayout添加自定义视图:LayoutParams忽略/自定义视图未正确定位

时间:2016-07-20 12:44:23

标签: android xml android-layout android-custom-view layout-inflater

我面临以下情况:

  • 我有一个RelativeLayout,其自定义布局将包含多个动态插入的视图。
  • 这些视图中的大部分都是自定义视图。

DialogFragment的自定义布局的父容器是addView(customView, params)

TL; DR

  • 我有一个带自定义布局的DialogFragment
  • params添加的自定义视图,其中params.addRule(RelativeLayout.BELOW, lastComponent.getId());已定义为lastComponent
  • 它们不在TextView下方。此外,任何其他规则都会被忽略,而它适用于核心RelativeLayout.LayoutParams

Popup.onCreateView()

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

    @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无效。以下显示了结果,但与上面的代码不匹配(添加了额外的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; } - 我将在稍后介绍): addressable

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

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

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

Popup.onCreateView()

li.inflate(layoutRes, container, false)

但那也失败了,给我一个空洞的结果: [Screenshot2]<遗漏,因为我的声誉不到10,哎呀。可能会在评论中添加它吗?

如果我在li.inflate(layoutRes, container, true) - >中将attachToRoot设置为true TextView(s)自定义组件可见,但LayoutParams似乎被忽略: [Screenshot]

奇怪的是,它在开始时适用于View(我在代码中只描述了一个,实际上有两个用于测试目的)。那么,我的自定义TextView是否应该像<body> <!-- Script 1 --> <script src="js/script1.js"></script> <!-- Script 2 --> <script src="js/script2.js"></script> </body> 一样处理,与“视图”也“差别不大”?

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

1 个答案:

答案 0 :(得分:0)

您可以使用LinearLayout代替RelativeLayout来添加另一个视图。并将方向设置为LinearLayout。