Android 当以编程方式添加到动态生成的relativelayout时,一个视图位于另一个视图之上
mDocView与cramperView重叠,就好像它在另一个视图上一样。我希望在顶部标题下显示mDocView My cramper.xml-Android 当以编程方式添加到动态生成的relativelayout时,一个视图位于另一个视图之上,android,view,android-relativelayout,overlap,Android,View,Android Relativelayout,Overlap,mDocView与cramperView重叠,就好像它在另一个视图上一样。我希望在顶部标题下显示mDocView My cramper.xml- 添加视图的布局参数 RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_
添加视图的布局参数
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
rlp.addRule(RelativeLayout.BELOW, R.id.linearLayout1);
mDocView.setLayoutParams(rlp);
layout.addView(cramperView);
layout.addView(mDocView);
setContentView(layout);
正如您所说的
ViewSwicher
在RelativeLayout
中,您在layout.addView(mDocView)
之后使用layout.addView(cramperView)(cramperView)
,因此它总是在cramper布局之后添加ReaderView
(扩展AdapterView)
解决方案是:
在xml文件中的
之后添加RelativeLayout
。像这样:
<RelativeLayout
android:id="@+id/docViewLayout"
android:layout_width="1190dp"
android:layout_height="1284dp"
android:layout_below="@+id/switcher"
android:layout_centerHorizontal="true" >
</RelativeLayout>
希望这有帮助。最后我添加了
setContentView(rlp)
,但它不起作用。消息-类型活动中的方法setContentView(int)不适用于参数(RelativeLayout.LayoutParams)。您不能setContentView
设置布局参数,只能设置布局。使用setContentView(yourParentLayout)
。谁让我们最后添加这个setContentView(rlp)。现在看我的答案。cramperView是我的整个xml文件。如果我把任何东西放在下面,就不要工作。结果是一样的。我将cramperView.getId()
替换为topHeader.getId()
,将layout.addView(cramperView)
替换为layout.addView(topHeader)
,但它在rlp.addRule(RelativeLayout.down,topHeader.getId())处给出了空指针异常。这些事情你必须小心。rlp.addRule(RelativeLayout.bellow,topHeader.getId())如果您想在topHeader视图的下方添加视图,则此操作将起作用。只需获得布局并将这些视图正确添加到该视图中..您的ViewSwitcher是否在RelativeLayout中?是的,它在RelativeLayout中。你能告诉我如何在线性布局标题下获取视图吗?
private View cramperView;
private LinearLayout topHeader;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
topHeader = (LinearLayout) findViewById(R.id.linearLayout1);
private ReaderView mDocView = new ReaderView(this); //ReaderView extends AdapterView
cramperView= getLayoutInflater().inflate(R.layout.cramper,null);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(cramperView);
layout.addView(mDocView);
setContentView(layout);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
rlp.addRule(RelativeLayout.BELOW, R.id.linearLayout1);
mDocView.setLayoutParams(rlp);
layout.addView(cramperView);
layout.addView(mDocView);
setContentView(layout);
<RelativeLayout
android:id="@+id/docViewLayout"
android:layout_width="1190dp"
android:layout_height="1284dp"
android:layout_below="@+id/switcher"
android:layout_centerHorizontal="true" >
</RelativeLayout>
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout docViewLayout = (RelativeLayout) mButtonsView.findViewById(R.id.docViewLayout);
docViewLayout.addView(mDocView);
layout.addView(cramperView);
setContentView(layout);