gpt4 book ai didi

java - 以编程方式将规则添加到布局中已有的 View - Android

转载 作者:行者123 更新时间:2023-11-29 05:02:40 26 4
gpt4 key购买 nike

如何向布局中已有的 View 添加规则?

我的 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());但是,如果我再次将 View 添加到布局中,它会给我一个 IllegalStateException(显然哈哈)。

那我该怎么做呢?

编辑

Java:

listView = (ListView) findViewById(R.id.list_view_id);
listView.setAdapter(myArrayAdapter);

layoutParams.addRule(RelativeLayout.BELOW,datePicker.getId());
relativeLayout.addView(listView,layoutParams);

它在 addView() 方法中抛出异常

最佳答案

您无法添加 View ,因为它已经添加。因此,您需要设置正确的布局参数。您可以创建新的或检索当前的并更改它们。结帐this post .

layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);

关于java - 以编程方式将规则添加到布局中已有的 View - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com