以编程方式将规则添加到布局中已有的视图 - Android
Adding Rule programatically to a View that is already in the layout - Android
如何向布局中已有的视图添加规则?
我的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>
我要添加layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
但是如果我再次将视图添加到布局中,它会给我一个 IllegalStateException(显然哈哈)。
那我该怎么做呢?
编辑
Java:
listView = (ListView) findViewById(R.id.list_view_id);
listView.setAdapter(myArrayAdapter);
layoutParams.addRule(RelativeLayout.BELOW,datePicker.getId());
relativeLayout.addView(listView,layoutParams);
它在 addView()
方法中抛出异常
正如 Matthias 建议的那样:
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);
您无法添加视图,因为它已经添加。因此,您需要设置正确的布局参数。您可以创建新的或检索当前的并更改它们。签出 this post.
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);
如何向布局中已有的视图添加规则?
我的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>
我要添加layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
但是如果我再次将视图添加到布局中,它会给我一个 IllegalStateException(显然哈哈)。
那我该怎么做呢?
编辑
Java:
listView = (ListView) findViewById(R.id.list_view_id);
listView.setAdapter(myArrayAdapter);
layoutParams.addRule(RelativeLayout.BELOW,datePicker.getId());
relativeLayout.addView(listView,layoutParams);
它在 addView()
方法中抛出异常
正如 Matthias 建议的那样:
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);
您无法添加视图,因为它已经添加。因此,您需要设置正确的布局参数。您可以创建新的或检索当前的并更改它们。签出 this post.
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);