我面临以下情况:
RelativeLayout
,其自定义布局将包含多个动态插入的视图。 DialogFragment的自定义布局的父容器是addView(customView, params)
。
TL; DR
params
添加的自定义视图,其中params.addRule(RelativeLayout.BELOW, lastComponent.getId());
已定义为lastComponent
TextView
下方。此外,任何其他规则都会被忽略,而它适用于核心RelativeLayout.LayoutParams
。Popup.onCreateView()
组件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>
一样处理,与“视图”也“差别不大”?
知道这里出了什么问题吗?
答案 0 :(得分:0)
您可以使用LinearLayout
代替RelativeLayout
来添加另一个视图。并将方向设置为LinearLayout。