[英]Adding Rule programatically to a View that is already in the layout - Android
How do i add a rule to a View that is already in my layout? 如何将规则添加到已在我的布局中的视图?
My XML: 我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylifeinformationsharedsocial.SexAcivity" >
<ListView
android:id="@+id/list_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I want to add layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
我想添加
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
But if i add the View again to the layout it gives me an IllegalStateException(obviously Haha). 但是,如果我再次将View添加到布局中,它会给我一个IllegalStateException(显然是哈哈)。
So how do i do that? 那我该怎么做?
EDIT 编辑
Java: Java的:
listView = (ListView) findViewById(R.id.list_view_id);
listView.setAdapter(myArrayAdapter);
layoutParams.addRule(RelativeLayout.BELOW,datePicker.getId());
relativeLayout.addView(listView,layoutParams);
It throws the Exception at the addView()
method 它在
addView()
方法中抛出异常
You can't add the view cause it's already added. 您无法添加视图,因为它已添加。 So, you need to set the correct layout params.
因此,您需要设置正确的布局参数。 You can either create new ones or retrieve the current ones and alter those.
您可以创建新的或检索当前的那些并更改它们。 Checkout this post .
看看这篇文章 。
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);
So as Matthias suggested: 正如马蒂亚斯所说:
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.